Updating a linux server is a good practice that can benefit your server’s security, functionality, and flexibility. This is important for several reasons:
- First, updating a linux server ensures that the server has the latest security patches and bug fixes, which can prevent potential attacks or errors from compromising the server’s performance or data.
- Second, updating a linux server can also improve the server’s functionality and compatibility with new software or hardware, which can enhance the server’s efficiency and usability.
- Third, updating a linux server can also provide access to new features or options that can make the server more customizable and adaptable to different needs or preferences.
But what’s the best way to ensure that your server remains up-to-date?
Cron jobs are tasks scheduled to run periodically on a computer system. They are often used for performing maintenance, backups, or other routine operations. Cron jobs are defined by a file called crontab, which specifies the commands to execute and the time intervals to do so. The crontab file can be edited using the crontab command or by using a text editor, and consists of six fields: minute, hour, day of month, month, day of week, and command.
A Cron job’s Structure
A cron job consists of two parts: a schedule and a command. The schedule defines when and how often the task should run, using a special format of minute, hour, day of month, month, day of week, and optional year. The command defines what action should be performed by the task, such as executing a script or sending an email. For example, a cronjob that runs every Monday at 10:00 am and prints “Hello World” to the standard output would look like this:
0 10 * * 1 echo “Hello World”
The structure of each section of the above line can be found in the table below.
|Minute (0-59)||Hour (0-23)||Day of the month (1-31)||
Month of the year (1-12)
|Day of the week (0-6)||Command to be executed|
Viewing All Cron jobs
Using the command below, one can view all existing Cron jobs. The command display jobs for the current user you are logged in as, which in this example is root.
To view other user Cron jobs, use the command crontab -u USERNAME -l while ensuring to change the “USERNAME” part with the actual username.
Editing and Adding Cron jobs
To add and schedule a cron job, use the command crontab -e to open the editor and begin entering a task.
Depending on your editor, the file will open for editing as shown below.
Now you can enter a new entry. For example, to have the system check for updates and update all available packages automatically every Wednesday at 2AM, then the following command would be used.
* 2 * * 3 sudo yum update -y > /dev/null 2>&1
Redirecting the Scheduled Task’s Output to a File
You can enter and schedule most commands and tasks including sending the output to a file using > to overwrite a file each time or append to it using >> . For example,
* 2 * * 3 sudo yum update -y > /home/hivelocity/update_status.txt – this command will send the output to the file we listed while overwriting the file each time the task runs.
* 2 * * 3 sudo yum update -y >> /home/hivelocity/update_status.txt – this command will send the output to the file we listed while adding the output to the existing file each time the task runs, which will have a file with all the outputs from every instance that the file ran.
Redirecting the Scheduled Task’s Output to an Email Address
To have the output of all the commands being run in the crontab file sent to an email address, ensure that at the top of the file the following line is added.
Note, make sure to change “email@example.com” to your email address within the double quotes, for example “firstname.lastname@example.org”.
Common cron job time structures
The listed items below can assist with the most common times used to run various tasks.
- * * * * * – This runs the command every minute.
- 0 * * * * – This runs the command every hour.
- 0 0 * * * – This runs the command every day at midnight.
- 0 0 * * 0 – This runs the command every Sunday at midnight.
- 0 0 1 * * – This runs the command on the first day of every month at midnight.
As well as the following,
- @reboot – Run once, at startup.
- @yearly – Run once a year
- @annually – (same as @yearly)
- @monthly – Run once a month
- @weekly – Run once a week
- @daily – Run once a day
- @midnight – (same as @daily)
- @hourly – Run once an hour
Creating a task can be complicated at first, so to make things easier, there are multiple sources online that will generate crontab lines based on your needs.
Two common generators are listed below.
– written by Pascal Suissa