Crontab Reboot_ How to Execute a Job Automatically at Boot?

Crontab is a widely used tool for scheduling tasks on Linux systems. It provides a simple way to automate routine tasks, such as backups, system maintenance, and data processing. While crontab typically schedules tasks based on specific time intervals, you can also execute jobs conditionally when certain conditions are met, such as when a file is created or when a process is running.

This article demonstrates how to use crontab to execute jobs conditionally and automatically at system boot on Linux systems.

Step 1: Identify the Condition

The first step is to identify the condition that triggers the execution of the job. There are many conditions that can be used to trigger a job, such as the creation or modification of a file, the presence of a running process, or the state of a system service.

For example, let's say we want to execute a script only when a specific file is created in the /tmp directory. We can use the inotifywait command to monitor the /tmp directory for file creation events, and execute our script when a new file is detected.

Step 2: Create the Script

Next, we need to create the script that will be executed when the condition is met. In our example, the script should perform some action when a new file is created in the /tmp directory. For simplicity, let's create a script that just echoes a message to the terminal.

Create a new file called myscript.sh and add the following code

#!/bin/bash
echo "New file created in /tmp directory"

Save the file and make it executable

chmod +x myscript.sh

Step 3: Monitor the Condition

Now we need to monitor the condition and execute our script when the condition is met. In our example, we want to monitor the /tmp directory for file creation events using the inotifywait command.

Open a new terminal window and type the following command

inotifywait -m /tmp -e create --format '%w%f' | while read file; do /path/to/myscript.sh; done

This command will monitor the /tmp directory for file creation events and execute the myscript.sh script when a new file is created.

Let's break down this command

  • The -m option tells inotifywait to monitor the directory continuously.

  • The -e option specifies the event we want to monitor, in this case create.

  • The --format option specifies the output format of the event. In our example, we want to output the name of the file that was created.

  • The | symbol is a pipe, which sends the output of the inotifywait command to the next command in the pipeline.

  • The while read loop reads each line of output from inotifywait and executes the myscript.sh script.

Replace /path/to/myscript.sh with the actual path to your script.

Step 4: Schedule the Job

The final step is to schedule the job using crontab. We want our script to be executed only when the condition is met, so we will use a conditional statement to check whether the script should be executed.

Open the crontab file for editing

crontab -e

Add the following line to the crontab file

* * * * * /usr/bin/bash -c 'if [ -f /tmp/*.txt ]; then /path/to/myscript.sh; fi'

This line schedules the job to run every minute. The command first checks whether there is a file with the .txt extension in the /tmp directory. If such a file exists, the myscript.sh script is executed.

Let's break down this command

  • The * symbols specify the time and date when the job should be executed. In this case, the job will run every minute.

  • The /usr/bin/bash -c part specifies the command to be executed. We need to use the full path to the bash executable to ensure that crontab can find it.

  • The if [ -f /tmp/*.txt ] part is a conditional statement that checks whether a file with the .txt extension exists in the /tmp directory.

  • The then keyword indicates that the script should be executed if the condition is true.

  • The /path/to/myscript.sh part specifies the path to our script.

Replace /path/to/myscript.sh with the actual path to your script.

Executing Jobs at Boot with @reboot

To execute a job automatically when the system boots, crontab provides the @reboot special time specification. This is particularly useful for starting services, initializing environment settings, or running maintenance scripts at startup.

Add the following line to your crontab to run a script at boot

@reboot /path/to/startup-script.sh

You can combine @reboot with conditional logic for more sophisticated boot-time automation

@reboot /usr/bin/bash -c 'if [ -f /etc/myapp.conf ]; then /path/to/startup-script.sh; fi'

Advanced Crontab Features

While conditional job execution is a powerful feature of crontab, there are many other advanced features that can help you automate your Linux system. Here are a few examples

Schedule Jobs at Specific Times

You can schedule jobs to run at specific times of day, weeks, or months. For example, the following command will run a script at 3:30 AM every day

30 3 * * * /path/to/script.sh

Run Jobs with Different Priorities

You can use the nice command to run jobs with different levels of priority. For example, the following command will run a script with low priority

0 0 * * * nice -n 19 /path/to/script.sh

Use Environment Variables

You can use environment variables to customize the behavior of your scripts

0 0 * * * ENV_VAR=value /path/to/script.sh

Common Use Cases

Use Case Crontab Example Description
Monitor Log Files 0 * * * * if grep -q error /var/log/syslog; then /path/to/alert.sh; fi Check system log for errors every hour
Service Availability */5 * * * * if ! pgrep apache2; then /path/to/restart.sh; fi Check if Apache is running every 5 minutes
File Cleanup 0 0 * * * find /tmp -type f -mtime +7 -delete Delete files older than 7 days
Database Backup * * * * * if [ $(mysql -N -e "SELECT COUNT(*) FROM mytable;") -gt 1000 ]; then /path/to/backup.sh; fi Backup when table exceeds 1000 rows

Key Points

  • Conditional execution allows jobs to run only when specific conditions are met

  • @reboot directive executes jobs automatically at system startup

  • inotifywait can monitor file system events for real-time automation

  • Environment variables and priorities can be set for advanced job control

  • Combine conditions with shell scripting for sophisticated automation workflows

Conclusion

Crontab provides powerful capabilities for both time-based and conditional job execution on Linux systems. By combining crontab with conditional statements, file monitoring tools like inotifywait, and the @reboot directive, you can create sophisticated automation workflows that respond to specific events and system states. This approach enables efficient system administration and reduces manual intervention requirements.

Updated on: 2026-03-17T09:01:38+05:30

504 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements