Understanding Cron Jobs and Cron Syntax
In this step, we will introduce Cron Jobs and the cron syntax.
Cron Jobs are "scheduled tasks" in the Linux system, commonly used to schedule commands that need to be executed periodically, such as regular data backups or cache clearing. They are called Cron Jobs because they use the cron
tool (crontab).
Cron Jobs are one of the most commonly used tools for system administrators, and they are great tools in themselves. However, if the "scheduled tasks" are set to run with higher user privileges (e.g., root user), they can potentially be exploited by attackers for privilege escalation.
The crontab command uses the following syntax:
* * * * * [ user ] File/Command
- - - - -
| | | | |
| | | | +----- Day of the week (0 - 7) (0 = Sunday)
| | | +---------- Month (1 - 12)
| | +--------------- Day of the month (1 - 31)
| +-------------------- Hour (0 - 23)
+------------------------- Minute (0 - 59)
The user
field is optional. If not specified, the command or script will be executed with the current user's privileges.
Note that only the root user can specify executing commands or scripts with other users' privileges.
For example, if we want to output the Apache error log every hour, we can use the following statement:
0 * * * * echo /var/log/lastlog
Since hour
, day
, month
, and week
are all set to *
, the scheduled task will be executed once every time the minute reaches 0, which is every hour.
What if we need to execute a command every two hours at the 15th minute? We can make a simple modification:
15 */2 * * * echo /var/log/lastlog
To understand the usage of cron
, let's go through an example.
-
Open the terminal and navigate to the /home/labex/project
directory. Then execute the following command to initialize the lab environment:
./env_setup1.sh
After successful initialization, you will see the cleanup.py
file and the trashDirectory
directory in the labex user's home directory. The trashDirectory
directory contains two files: error.log
and readme.txt
.
Our lab goal is as follows: Use crontab
to add a scheduled task that calls the cleanup.py
script every 1 minute to clear all data in the /home/labex/project/trashDirectory
directory. The cleanup.py
script simply calls the rm
system command through the os.system()
function to clear the /home/labex/project/trashDirectory
directory.
-
Next, use the following command to add a scheduled task in crontab:
echo "* * * * * root python /home/labex/project/cleanup.py" | sudo tee -a /etc/crontab > /dev/null
The above command adds a scheduled task to the crontab file. The task will be executed every minute
, and the cleanup.py
script will be executed with root privileges.
-
Since the cron service is disabled by default on our lab machine, we need to manually start it with the following command:
sudo service cron start
Expected output:
* Starting periodic command scheduler cron
After about a minute
, if we enter the /home/labex/project/trashDirectory
directory and use the ls
command, we will see that the data in the directory has been cleared, indicating that the cron job is executing correctly.