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 viachmod
- I checked that cron jobs are indeed being ran: to verify this, I added some lines to
file.sh
that append messages to a filedebug.txt
. The messages are appearing indebug.txt
, so I knowfile.sh
has been ran by cron. - I checked that it was not a problem with environment variables. I exported both
PATH
andSHELL
both to crontab and tofile.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.