Scheduling Task on Linux: Part 02

Scheduling Task on Linux: Part 02

4. Scheduling a Recurring User Job

Understanding Recurring Jobs

Recurring jobs are those type of jobs that runs at specific intervals of time, and they are divided into groups of jobs like daily, weekly and monthly jobs. As different to the deferred jobs which are executed just once at a particular kind of time, the repetitive jobs execute according to schedule. This helps in running programs that need consistent automation in the background, such as backup, and system updates.

Steps to Schedule a Recurring User Job

To schedule a recurring user job, follow these simple steps:

1. Open Terminal: Access your command line interface.

2. Edit Crontab: Type crontab -e to open your crontab file.

3. Add Recurring Job: Enter the desired schedule and command using cron syntax.

For example, to run a script daily at 2 AM:

Figure 11: Example syntax of a crontab file.

4. Save and Exit: Save the changes and exit the editor. Your job is now set to run automatically on the specified schedule!

This setup ensures your tasks are handled consistently without any manual effort.

Daily, Weekly, Monthly Tasks

To schedule tasks daily, weekly, or monthly using cron:

Daily: Use 0 2 * /path/to/script.sh to run a task every day at 2 AM.

Weekly: Use 0 2 1 /path/to/script.sh to run a task every Monday at 2 AM.

Monthly: Use 0 2 1 /path/to/script.sh to run a task on the 1st of every month at 2 AM.

These settings automate your tasks according to your preferred frequency.

Using Crontab Command

Creating the Script

To create a script for your deferred job, follow these steps:

1. Open a Text Editor: Use a text editor like Nano or Vim. For example, type nano /path/to/script.sh in the terminal.

2. Write Your Script: The commands that you want to execute in this task can be entered. For example:

Figure 12: Creating a command to write a message on a .txt file at a specific time.

3. Save the Script: To save and exit in Nano, press CTRL + X, then Y to confirm your changes, and hit Enter to save.

4. Make the Script Executable: Run chmod +x /path/to/script.sh to give the script execute permissions.


Figure 13: Making the file executable.

Your script is now ready to be scheduled as a recurring job in your crontab.

Adding the Job to Cron

“0 16 /path/to/tea_time.sh


Figure 14: Adding the job in crontab.

Result:

Figure 15: Showing the message when the script is executed.

5. Scheduling a Recurring System Job

5.1 Difference Between user and system Cron Jobs

5.2 Steps to schedule a recurring System Job

To schedule a recurring system job:

1.???? Create the script:

Write the script that you want to schedule, for example, a script to clean the temporary files.

Clean_Temp.sh:

Figure 16: Creating a command to clean /tmp directory files.

2. Make the script Executable:

Change the script permissions to make it executable:


Figure 17: Making the file executable.

3. Edit the system Crontab file:

Open the system-wide crontab file for editing:

Figure 18: Editing the crontab file.

4. Add the Cron Job:

Add a line to schedule the script. To run the script every day:

Figure 19: Adding the job in crontab.

5. Save the Exit:

Save your changes and exit the text editor:

Press “Ctrl + O” to save.

Press “Ctrl + X” to exit.

Adding the Job to Cron

This script can be run on CLI, and you can also include it among your cron jobs. This makes it possible for the script to be executed at the specified intervals without the need for the user’s input.

Results:

Figure 20: All the files are deleted.

6. Common Cron Job Management Tasks

The efficient management of cron jobs is critical for managing the automation and the scheduled tasks in your system. Below are some of the typical jobs of managing cron with practical scripts and examples to consider in the course of their management.

Example Script to View Scheduled Jobs

To view the list of cron jobs scheduled for the current user, you can use the following script:

View_Jobs.sh:


Figure 27: Creating a script to remove the scheduled cron jobs.

The ‘crontab -r’ command deletes the crontab files, thereby removing all the scheduled jobs by using this script with caution, as it will remove all the exiting cron jobs without any confirmations.

Explanation Points:

1.???? Viewing Scheduled Jobs: Use the view_jobs.sh script to check out which cron jobs are currently scheduled for your user. This one is useful when it comes to auditing and debugging.

2.???? Editing Scheduled Jobs: Use the edit_jobs.sh script to automatically open the crontab file in an editor. This enables one to create a new job, change the existing job, or even change the timings of the job.

3.???? Removing Scheduled Jobs: The remove_jobs.sh script comes in handy while erasing all the scheduled jobs, whether for cleaning purposes or in other situations where one is configuring cron jobs from scratch.

7. Troubleshooting Cron Jobs

Common Issues and Solutions: you might encounter while using cron jobs, one being the script not running correctly. One issue is permissions. Ensure your scripts have the right execute permissions by using the chmod +x /path/to/your/script.sh, Another problem is the path. Always use absolute paths in your scripts to avoid which can be combined with or where the files reside.

Checking Cron Logs: If your cron job does not run as planned, inspect the cron logs. These logs are available on most systems. You may examine them in /var/log/cron or /var/log/syslog. Look for anything related to cron to see if there are any errors or other messages to describe why the job could not be run.

8. Best Practices for Using Cron Jobs

Security Consideration: Security is key with cron jobs. Only schedule jobs that you have faith in and ensure the scripts are secure. Use the principle of least privilege, i.e. your scripts must run with the minimum rights absolutely required!

Effective scheduling tips: Do not schedule too many tasks at a time, as it can lead to performance issues. Distribute them evenly and alleviate your system load. Ensure that tasks are non-overlapping, and space the intervals between them in a way that ensures they will be completed efficiently. The key is implementing these tips on how to have efficient and reliable cron jobs.


要查看或添加评论,请登录

Manula Bandara的更多文章

  • Scheduling Task on Linux: Part 01

    Scheduling Task on Linux: Part 01

    Introduction Welcome to the world of Linux automation! In our fast-paced digital era, efficiency is key, and mastering…

    10 条评论

社区洞察

其他会员也浏览了