DEV Community

Shuaib
Shuaib

Posted on • Originally published at shuaib.org on

Setting up Cron jobs to run bash scripts

Setting up Cron jobs to run bash scripts

The setup of cron jobs is fairly easy. However, I ran into problems running shell scripts using 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.

Setting up Cron jobs to run bash scripts

Ensure you 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 every 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 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. 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 you environment.



#Identify what in the path variable in the context of your script
* * * * * echo $PATH > ~/cron.log

#Identify the shell being use in your script
* * * * * echo $SHELL > ~/cron.log

#Identify the working directory your cron job natively executes in
* * * * * echo $PATH > ~/cron.log


Enter fullscreen mode Exit fullscreen mode

View the outputs of these logs by inspecting the cron.log file the logs are output to cat ~/cron.log.

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


Enter fullscreen mode Exit fullscreen mode

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.

Top comments (3)

Collapse
 
mdhesari profile image
Mohammad Fazel

Great article.

Well I am a little bit confused of how exactly cron jobs work and perform operation, any reources or tips you can give me to learn more?

In addition, how we can make use of them properly on windows or mac?

Collapse
 
siddharthshyniben profile image
Siddharth

For how it works, check this out.

On mac, it should work, but I don't know about windows.

Collapse
 
vasudevanep profile image
Vasudeva • Edited

Good Article!
Most of the people use it in the Linux environment and have no idea about the default option called crontab which is given by linux itself is more powerfull.

well you have given a good example how do we log the crons output in different file that is accessable if there is any problem in the cron?