The setup of cron jobs is fairly easy. However, I ran into problems running shell scripts that create files as outputs. This post covers some of the gotchas to keep in mind.
How to setup Cron jobs
To setup a cronjob, you use a command called
crontab. If you execute
crontab -l you will see a list of currently installed cron jobs. Jobs are defined in a text file using crontab syntax that you can find or generate at https://crontab-generator.org/. Each line is a job that will be executed. To add/modify/delete a job run
crontab -e and perform the deed.
Running a job as a root user
If you want to run a job as a root user, run
sudo crontab -e to define a job. Once you save the crontab file, you will see an output on the console saying
crontab: installing new crontab which confirms you newly defined changes have been configured.
Ensure your shell script is running with the right shell and environment variables
When you normally run a shell script, it runs under the context of your user profile's shell settings. So it knows which shell executor to use, the programs that are available in your PATH environment variable etc. However, when you run the same script with crontab, you may have a very different context/environment variables. It's best to specify these explicitly so that others and your future self can understand your state of mind and thinking if they ever look at it.
You can determine the correct shell location by running
which bash to get the shell location (in my case its bash on Ubuntu but you may have a different shell). Similarly, if you are using other programs such as docker, you can run
which docker to get the exact file path of the program. Then in your shell script, instead of just using
docker you specify the exact docker file you want the script to run. The alternative is the set environment variables in your script to keep the configuration dynamic, it all depends on your situation and use case.
Specify absolute paths in shell script outputs
If your script is creating outputs, its a good idea to specify these in absolute terms. There is some confusion around what is the working directory where the files created by scripts run by crontab are placed. Within the script, you should explicitly CD into the directory you'd like to work in. Here are a few cron jobs you can run to get an idea of your environment.
#Identify whats in the path variable in the context of your script * * * * * echo $PATH > ~/cron.log #Identify the shell being used in your script * * * * * echo $SHELL > ~/cron.log #Identify the working directory your cron job executes in by default (usually user home directory) * * * * * echo $(pwd) > ~/cron.log
View the outputs of these logs by inspecting the
cron.log file the logs are output to
Make sure your script is executable and has the right permissions
Set a script to be executable by running
chmod +x myscript.sh
Set the correct ownership by running
chown myusername: myscript.sh
Inspect cron job runs
Run the following to see if the cronjob you've defined actually runs.
sudo grep CRON /var/log/syslog
Are there any other common mistakes you ran in to as a beginner when defining cronjobs to run shell scripts or cron jobs in general. Let me know in the comments.