Setting up Cron jobs to run bash scripts
Tags: linux, cron
Published: 2020-04-05 16:10:50

---
title: Setting up Cron jobs to run bash scripts
published: true
date: 2020-04-05 16:04:14 UTC
tags: Linux,Cron
canonical_url: https://shuaib.org/setting-up-cron-jobs-to-run-bash-scripts/
---

 ![Setting up Cron jobs to run bash scripts](https://images.unsplash.com/photo-1548672150-6d77dcc2639d?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=2000&fit=max&ixid=eyJhcHBfaWQiOjExNzczfQ)

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/](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](https://blog.shuaib.org/content/images/2020/04/image-7.png)

### 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
```

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
```

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.