Linux: Crontab Guide

·

3 min read

Linux: Crontab Guide

Crontab is a Linux utility that allows you to schedule commands or scripts to run automatically at specific times or intervals. It is a powerful tool that can be used for a variety of tasks, such as backing up data, sending email notifications, and running system maintenance tasks.

Important Commands

  1. crontab -l

The command crontab -l is used to display the current crontab schedule for the logged-in user. When you run this command, the system will display all the scheduled tasks in the user's crontab file.

2. crontab -e

The command crontab -e is used to edit the crontab schedule for the current user. When you run this command, the system will open the default editor (usually vi or nano) and allow you to edit the crontab file.

3. crontab -r

The command crontab -r is used to remove the crontab schedule for the current user. When you run this command, the system will delete the user's crontab file, which will remove all scheduled tasks for that user.

4. sudo tail /var/log/syslog | grep -i cron

The command will display the last few lines of the syslog file that contain the word "cron", ignoring case.

Examples:

  1. Shell script for pinging a host and logging the results to a file

Cron Expression to trigger a Job for every minute and append in ping.log file

master@master:~/workspace$ cat ping_shellscript.sh
#!/bin/bash
host="google.com"
logfile="/home/master/workspace/ping.log"
echo "Pinging $host ..."
ping -c 3 "$host" >> "$logfile"
master@master:~/workspace$ crontab -l
# Edit this file to introduce tasks to be run by cron.
* * * * * /home/master/workspace/ping_shellscript.sh
master@master:~/workspace$ cat ping.log
PING google.com (142.250.195.46) 56(84) bytes of data.
64 bytes from maa03s37-in-f14.1e100.net (142.250.195.46): icmp_seq=1 ttl=128 time=19.4 ms
64 bytes from maa03s37-in-f14.1e100.net (142.250.195.46): icmp_seq=2 ttl=128 time=28.3 ms
64 bytes from maa03s37-in-f14.1e100.net (142.250.195.46): icmp_seq=3 ttl=128 time=35.0 ms

--- google.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
rtt min/avg/max/mdev = 19.388/27.573/34.984/6.390 ms
PING google.com (142.250.195.46) 56(84) bytes of data.
64 bytes from maa03s37-in-f14.1e100.net (142.250.195.46): icmp_seq=1 ttl=128 time=20.6 ms
64 bytes from maa03s37-in-f14.1e100.net (142.250.195.46): icmp_seq=2 ttl=128 time=27.7 ms
64 bytes from maa03s37-in-f14.1e100.net (142.250.195.46): icmp_seq=3 ttl=128 time=26.5 ms

2. Download data and move to the destination directory

Cron Expression to trigger a Job for every minute and download data and move to the destination directory

#!/bin/bash
# Set the URL and destination directory
URL=https://getsamplefiles.com/download/tar/sample-1.tar
DEST_DIR=/home/master/workspace/data

# Download the file to a temporary location
backup_name=$(date +%Y-%m-%d_%H%M%S).tar.gz
#TMP_FILE=$(mktemp)
wget -qO $backup_name $URL || { echo "Failed to download file from $URL"; exit 1; }

# Extract the contents of the file to the destination directory
mkdir -p $DEST_DIR || { echo "Failed to create directory $DEST_DIR"; exit 1; }
cp $backup_name $DEST_DIR || { echo "Failed to backup file to $DEST_DIR"; exit 1; }

# Clean up the temporary file
rm $backup_name

echo "File downloaded and moved to $DEST_DIR successfully."
master@master:~/workspace$ crontab -l
# Edit this file to introduce tasks to be run by cron.
* * * * * /home/master/workspace/download_data.sh

End.

Please provide your valuable comments.