In Linux, cron jobs can be used to run automated tasks at a specific time or even on a specific date. This could be a backup or a simple script you have created. Cron jobs are stored in the crontab file and can be edited by using nano. When adding a cron job it’s important to get the syntax correct or your cron job might not run at the correct time. Always ensure you know the file you want to execute using a cron is from a trustworthy source. Badly written scripts sometimes bring down servers.
View Current Cron Jobs
You can view your current cron jobs by inspecting the crontab file. The easiest way is via cat
Here you will see the file with the syntax at the top with a list of cron jobs at the bottom. Your cron jobs will look a little like this
* * * * * root /path/to/cron/file.sh > /dev/null 2>&1
From this cron job we can see the job is set to run every 1 minute which is shown via the five * at the start of the command and the cron is being run as root. It’s worth pointing out that unless your cron requires root privileges then it’s always best to add cron jobs to a normal Linux user account.
Each time a cron runs then a notification will be sent to the cron users default account. If you have a cron that is running every minute then this can cause a lot of mail to build up. You can use the >/dev/null 2>&1 command to stop this. Just append it to the end of your cron command like in our example above.
When you are ready to add your cron job use your favourite text editor like nano to edit the file and place your cron job.
How was this article?
You might also like
More from All About Linux
Update CentOS 7 Kernel in your KVM virtual server or dedicated server runs one of the most important functions of …
Setup a website on an Ubuntu Server using Apache Virtual Hosts (Name-Bassed Hosting) In this guide, we will show you how …