If you're looking for how to make a crontab run a bash script, you've come to the right spot. 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.
A cron job (or crontab job) in Linux is like a scheduled task on Windows that executes a command or a process on a specific schedule that you define. Cron jobs are defined using the crontab command (aka cron command) via the command line. For example, you might want to run a python script, a particular Linux command, or even a bash file containing those commands on a defined schedule. The schedule is defined in what is known as a crontab file. A crontab file (or crontab script) defines exactly when a job should run. The cron file (or crontab file) contains one crontab entry per line that runs a cron task. The crontab command is used to control the cron daemon (or cron service,
crond), which is the magician in the background that actually runs and manages the tasks you defined in the background. So without further ado, let's create your first cron job.
How to setup Cron jobs
To set up 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 your 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 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 it's 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, it's 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 into as a beginner when defining cronjobs to run shell scripts or cron jobs in general? Let me know in the comments.