0

Hello I am trying to execute a sh script via cronjob. My script:

#!/bin/bash
#!/usr/bin/expect -f

IP=$(hostname -I | awk '{print $2}')
echo $IP

echo "Verbindung zum Server aufbauen"

So what I am trying to do is, I need the IP of my Raspberry Pi and start a server via ssh. When I execute the script via terminal it works fine.

AdminBee
  • 21,637
  • 21
  • 47
  • 71
matt
  • 1
  • Please present the code of your script using "code sample" formatting. Currently it's not clear how the script actually looks like - for example there is `#!/bin/bash #!/usr/bin/expect -f` on the first line, which seems obviously wrong. – raj Jan 20 '21 at 21:23
  • Welcome to the site. As you can see from some comments (where due to lack for formatting contributors misinterpreted your script has having _two_ shebangs in the first line), please be sure to use the formatting tools when formulating your question. Also if you say "when I execute the script via terminal": Please edit your question to post the exact command-line (do you say `./script.sh` , or `bash script.sh`, or whatever), and state which user you are when running the script via terminal. – AdminBee Jan 21 '21 at 10:01

1 Answers1

1

Using a double shebang might not work (#!/bin/bash #!/usr/bin/expect -f)

You need to set the PATH variable for cronjobs (see Where is cron's PATH set? )

This should work

#!/bin/bash
PATH=/usr/bin:/bin
hostname -I | awk '{print $2}' 
echo "Verbindung zum Server aufbauen"
Michael D.
  • 2,820
  • 16
  • 24