Cao Yi

Adding a Cron Task Using a Script

Index

To add a cron task item using a script, you can utilize the following script:

#!/bin/bash

# Define the crontab entry
CRONTAB_ENTRY="* * * * * echo \"hello, world!\" >> log_.txt 2>&1"

# Write the crontab entry to a temporary file
echo "$CRONTAB_ENTRY" > /tmp/my_cron

# Load the temporary crontab file
crontab /tmp/my_cron

# Remove the temporary file
rm /tmp/my_cron

Please note that running the above script will reset the crontab list, removing all previous items.

If you wish to create a log file for each day, you can replace the line:

CRONTAB_ENTRY="* * * * * echo \"hello, world!\" >> log_.txt 2>&1"

with:

CRONTAB_ENTRY="* * * * * echo \"hello, world!\" >> log_\$(date +\%Y\%m\%d).txt 2>&1"

Take note of how the Linux command is included within the crontask item above.

To view the current cron task list, you can use the command:

$ crontab -l
* * * * * echo "hello, world!" >> log_$(date +\%Y\%m\%d).txt 2>&1

The Slash in Cron Task

CRONTAB_ENTRY="* * * * * echo \"hello, world!\" >> log_\$(date +\%Y\%m\%d).txt 2>&1"

Note that the slash before the dollar sign is essential in this command. If the slash is removed:

CRONTAB_ENTRY="* * * * * echo \"hello, world!\" >> log_$(date +\%Y\%m\%d).txt 2>&1"

The updated cron task list will appear as:

$ crontab -l
* * * * * echo "hello, world!" >> log_20230711.txt 2>&1

The cron task list is crucial for scheduling and automating tasks on a system. It is important to ensure the correct syntax and formatting in the cron task list to ensure proper execution and logging of tasks.