Crontab Reboot: How to Execute a Job Automatically at Boot

本文指导如何使用Linux的CronDaemon通过crontab在系统重启时安排任务。讲解了如何设置定时执行,包括在启动时运行、延迟执行以及移除任务的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Introduction

The Cron daemon is a Linux utility used for scheduling system tasks and processes. It uses cron tables (crontab) to store and read scheduled jobs.

This tutorial will cover how you can use crontab to schedule jobs to be run at system reboot.

Prerequisites

Crontab Command Overview

With the crontab command, you have full control of when and how jobs are executed. Use crontab to set job execution time down to the minute, without the need for looping and timing logic in the task.

crontab has low resource requirements since it doesn’t reserve system memory when it isn’t running.

Crontab on Boot: Run a Cron Job at Boot Time

Open the cron task list by using the following command:

crontab -e

If you have multiple text editors installed, the system prompts you to select an editor to update the cron task list with. Use the number in the brackets to choose your preferred option. We will be using the default option, Nano.

Opening cron jobs ist with the crontab command

Note: Leaving the field blank and pressing enter chooses the first option available.

To run a cron job at every system boot, add a string called @reboot to the end of the task list. The job defined by this string runs at startup, immediately after Linux reboots.

Use the following syntax when adding a @reboot string:

@reboot [path to command] [argument1] [argument2] … [argument n]
@reboot [part to shell script]

Note: Always use the full path to the job, script, or command you want to run, starting from the root.

Press Control + X to exit Nano, then Y and Enter to save any changes you made.

For example, if we wanted to have the system date written in a file called date.txt when Linux restarts, we would add the following string:

@reboot date >> ~/date.txt

If we wanted to run the backup shell at reboot, we would add:

@reboot /root/backup.sh

Updating the cron job list

Note: In some cases, the crond service needs to be enabled on boot for the configuration to function.
To check if the crond service is enabled, use:

sudo systemctl status cron.service

To enable this service, use:

sudo systemctl enable cron.service

Run a Cron Job at Boot With Delay

To run a job with a delay after the system reboots, use the sleep command when adding the @reboot string:

@reboot sleep [time in seconds] && [path to job]

If you want to create a text file with the system date five minutes after reboot, add:

@reboot sleep 300 && date >> ~/date.txt

Remove a Reboot Command

Each @reboot string you add to the cron task list runs a job every time Linux restarts. If you no longer wish to run a job, remove it from the task list.

To do this, open the task list using the crontab -e command. Scroll down to the bottom to review the jobs you added.

To remove a task from the list, delete the appropriate line from the appropriate string. Press Control + X to exit Nano, then Y and Enter to save changes.

Note: Learn more about the Linux at command, the alternative for cron job for scheduling jobs.

Conclusion

After following this tutorial, you understand how to use crontab to schedule jobs to run at system reboot.

For more ways to schedule jobs in crontab, check out our guide to setting up cron jobs.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值