I am a highly skilled and experienced administrator with a strong background in both Windows and Linux systems. I have a deep understanding of network infrastructure, security, and system administration, and am able to effectively troubleshoot and resolve a wide range of technical issues. I am also well-versed in various scripting languages, including PowerShell and Bash, and am able to automate repetitive tasks and improve system efficiency.

Full width home advertisement

Post Page Advertisement [Top]

 



Schedule Future Tasks in Linux Using at Command and Crontab



Schedule Non repetative tasks with at command:

 

Commands used with at:

at : execute commands at specified time.

atq : lists the pending jobs of users.

atrm : delete jobs by their job number.

1. Schedule first job using at command

Below example will schedule “sh backup.sh” command to be executed on next 9:00 AM once.

 

at 9:00 AM

at sh backup.sh

at ^d

job 3 at 2013-03-23 09:00

Use ^d to exit from at prompt.

You can also use the following option to schedule a job. The below command will run “sh backup.sh” at 9:00 in the morning.

 

echo "sh backup.sh" | at 9:00 AM

2. List the scheduled jobs using atq

When we list jobs by root account using atq, it shows all users jobs in the result. But if we execute it from a non-root account, it will show only that users jobs.

 

atq

 

3   2013-03-23 09:00 a root

5   2013-03-23 10:00 a rahul

1   2013-03-23 12:00 a root

Fields description:

First filed: job id

Second filed: Job execution date

third filed: Job execution time

Last field: User name, under which job is scheduled.

 

3. Remove scheduled job using atrm

You can remove any at job using atrm with their job id.

 

atrm 3

atq

 

5   2013-03-23 10:00 a rahul

1   2013-03-23 12:00 a root

 

 

4. Check the content of scheduled at job

atq command only shows the list of jobs but if you want to check what script/commands are scheduled with that task, below example will help you.

 

at -c 5

In the above example, 5 is the job id.

 

For more information please visit: (reference)

https://tecadmin.net/one-time-task-sc...

 

============================================================

Schedule repetative tasks with Crontab:

 

# crontab -e

SHELL=/bin/bash

MAILTO=root@example.com

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin

 

# For details see man 4 crontabs

 

# Example of job definition:

# .---------------- minute (0 - 59)

# |  .------------- hour (0 - 23)

# |  |  .---------- day of month (1 - 31)

# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...

# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat

# |  |  |  |  |

# *  *  *  *  * user-name  command to be executed

 

# backup using the rsbu program to the internal 4TB HDD and then 4TB external

01 01 * * * /usr/local/bin/rsbu -vbd1 ; /usr/local/bin/rsbu -vbd2

 

# Set the hardware clock to keep it in sync with the more accurate system clock

03 05 * * * /sbin/hwclock --systohc

 

# Perform monthly updates on the first of the month

# 25 04 1 * * /usr/bin/dnf -y update


No comments:

Post a Comment

Bottom Ad [Post Page]