0

I'm an linux newbie, so please forgive my ignorance.

I'm trying to get a file.sh to run in crontab, so it runs every day at the same time. When I run the script on the terminal, it works fine. But it does not work in crontab. The file looks like this:

echo 'password' | snx
python3 /home/absolute/path/to/script.py
snx -d

The first line connects to a VPN. The second line runs a python script (that gets data via a request, and then uploads the data to a database). The third line disconnects from the VPN.

As I said, it works fine when running on the terminal. But when I run crontab -e and add the job as:

* * * * * /home/absolute/path/to/file.sh

I know it is not running properly (since the data is not uploaded to the database, so the python script is definitely not doing what it is supposed to do).

I have already tried:

  • I made file.sh executable via chmod
  • I checked that cron jobs are indeed being ran: to verify this, I added some lines to file.sh that append messages to a file debug.txt. The messages are appearing in debug.txt, so I know file.sh has been ran by cron.
  • I checked that it was not a problem with environment variables. I exported both PATH and SHELL both to crontab and to file.sh. It still doesn't work.
  • Add shebang #!/bin/sh (and others) at beginning of the file.
  • Call snx with absolute path.

I think it may be something to do with SNX not working on crontab. This issue points to that possibility:

https://stackoverflow.com/questions/54221762/automate-snx-restart-with-crontab

But I'm not sure how to use that information (again, I'm a linux newbie). I tried the exact solution proposed there (i.e. I added those lines at the top of my file) but it is still not working.

What else can I try to make the script work on cron? Is there another way to schedule scripts to run periodically? It seems strange to me that cron behaves so differently from the terminal.

4
  • Your shell file is missing the shebang
    – elaforma
    Dec 23, 2019 at 1:27
  • Thanks! Do you mean adding #!/bin/sh at the beginning of the file? Could you explain a little more how that would fix my problem?
    – acrendic
    Dec 23, 2019 at 1:32
  • Consider using an absolute path to ‘echo’ as crown doesn’t have access to the same environment variables like PATH Dec 23, 2019 at 1:51
  • Thanks! I tried that before, I will update my question to add that
    – acrendic
    Dec 23, 2019 at 2:00

1 Answer 1

0

Change your script to:

#!/bin/sh
echo 'password' | snx
python3 /home/absolute/path/to/script.py
snx -d

A "shebang" is required for Linux to know how to execute your scripts. Otherwise, there is no (surefire) way for the Kernel to know whether you are writing Shell, Python, Perl, or a whole range of other languages.

1
  • Thanks for the answer! And for the explanation about shebang. Sadly, it's still not working :/
    – acrendic
    Dec 23, 2019 at 1:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .