LinuxScripting

Linux Cron Jobs – How to add a Cron Job In Linux Servers

Linux Cron Jobs are ways you can automate tasks. You could set a cron job to run a script on a specific date or even time. Crons are a great way of doing your daily banking if you have a shop or if you want to run a bash script to clear out your junk mail.

When adding a cron job it’s important to get the syntax correct. If it’s wrong your Linux 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 Linux Crons Jobs

You can view your current cron jobs by inspecting the crontab file. The easiest way is via cat.

cat /etc/crontab
Linux Cron Job

Here you will see the file with the syntax at the top with a list of cron jobs at the bottom. Your cron will look a little like this;

* * * * * root /path/to/cron/file.sh > /dev/null 2>&1 

The example cron job is set to run every 1 minute. This is shown via the five * at the start of the command. 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 Linux Cron Jobs to a normal Linux user account. When a Linux cron runs, then a notification will be sent to the cron user’s 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 Linux cron job. Use your favourite text editor like Nano to edit the file and place your cron job.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button