Step 1: Access the Crontab Configuration.
Open a terminal on your CentOS 7 server.
Step 2: Edit the Crontab File.
a) Use the following command to open the crontab file for the current user:-
# [ crontab -e ]
b) If you want to edit the crontab for a specific user, you can use:-
# [ crontab -e -u username ]
c) This will open the crontab file in the default text editor (usually 'vi'
or 'nano'
).
Step 3: Add a New Cron Job.
a) Each line in the crontab file represents a single cron job, and the format is as follows:-
# [ minute hour day month day_of_week command_to_run ]
Here’s a quick reference for each field:-
‘minute’: 0-59
‘hour’: 0-23
‘day’: 1-31
‘month’: 1-12
‘day_of_week’: 0-6 (Sunday is 0, Monday is 1, and so on)
‘command_to_run’: The command or script you want to schedule
b) For example, to run a script every day at 3:30 AM, add the following line:-
# [ 30 3 * * * /path/to/your/script.sh ]
c) Save and exit the editor.
Step 4: Verify the New Cron Job.
a) You can list the current cron jobs using:-
# [ crontab -l ]
b) Make sure your new cron job is listed.
Step 5: Restart the Cron Service (if necessary).
a) In most cases, the cron service will automatically pick up changes. However, if you encounter issues, you can restart the cron service:-
# [systemctl restart crond ]
b) Ensure that the service is running:-
# [ systemctl status crond ]
Conclusion:- Your cron job is now scheduled to run based on the specified time and frequency. You can add additional cron jobs or modify existing ones using the same 'crontab -e'
command.