- Crontab Reboot: How to Execute a Job Automatically at Boot
- Crontab Command Overview
- Crontab on Boot: Run a Cron Job at Boot Time
- Run a Cron Job at Boot With Delay
- Remove a Reboot Command
- @reboot is not working in CRON
- 3 Answers 3
- how can I schedule a reboot in Linux?
- 6 Answers 6
- Linux – Auto-Reboot and Shutdown with Cron Jobs – Task Scheduler alternative How to configure a daily or weekly automatic shutdown & reboot of your machine using cron job in Linux CentOS, Ubuntu, Debian, RHEL
- Cron web-based GUI
- Troubleshooting
- crontab’s @reboot only works for root?
- 9 Answers 9
- Additional evidence
- Example
- Take aways
- Debugging crond
Crontab Reboot: How to Execute a Job Automatically at Boot
Home » Backup and Recovery » Crontab Reboot: How to Execute a Job Automatically at Boot
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.
- A system running Linux
- Access to a command line/terminal window
- A user account with root or sudo privileges
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:
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.
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:
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:
If we wanted to run the backup shell at reboot, we would add:
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:
To enable this service, use:
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:
If you want to create a text file with the system date five minutes after reboot, add:
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.
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.
Источник
@reboot is not working in CRON
I’m trying to run a shell-script and a command at the start of my Ubuntu server.
Here is my CRON
What I’m getting in the logs: grep CRON /var/log/syslog
My start.sh . It’s for launching an Unturned game server. I don’t think this script is important but I guess I should show you.
The thing is that if I execute ./check.sh from /home/steam It works fine. The bad news is that the @reboot doesn’t work for me when I reboot my VPS.
screen -list doesn’t throw anything if I reboot.
I’ve tried multiple things but didn’t work, the last thing I changed was adding -d parameter in the screen commands so the server wouldn’t need a terminal to write down the start-up.
I’m not sure how much can be done here to make @reboot work as expected.
How can I make my scripts run on boot? Are there any other alternatives to CRON ‘s @reboot ?
Thanks in advance.
3 Answers 3
Try man 5 crontab . If your crontab supported, you should see @reboot, @yearly, @monthly.
then try add some sleep for moment may can help.
Take a look at the systemd.service manpage. It describes how to configure systemd to manage a service. I am sure you will find examples for your system in /usr/lib/systemd/system or similar paths.
In your case, the service would look somewhat like this:
Источник
how can I schedule a reboot in Linux?
I’m new in Linux and I want to schedule a reboot at midnight. How should I do it?
Edits:
I’m sorry I didn’t put the complete details. I want a reboot every 3rd Saturday of the month at 23:30.
I don’t know what’s wrong but I cannot find crontab. What I have is cron.d ; cron.daily ; cron.weekly ; cron.monthly ;
I’m sorry for the noob question. Pls help me. Thanks.
6 Answers 6
Type shutdown -r 0:00 and it will reboot at midnight.
If you want to reboot each night, add a cron entry using crontab -e as root to run shutdown -r each midnight
Adding this entry to /etc/crontab should do:
Another option is the at command, available on many Linux distributions. See the man page for more info, but the general syntax for your purpose would be:
To quote the OS X man page:
at — executes commands at a specified time
Sound like what we’re talking about. 😉
As far as I know, you cannot use cron to schedule tasks for «last Friday of each month» or «third Thursday in each month». What you can do, however ugly it seems, is to have a script run every Saturday at 23:30 and then have this script determine if this particular Saturday is the third Saturday of the week (can be done using date and maybe cal commands).
I hope this helps. I have not found an elagant solution to this problem. I found this thread, because I was searching for a solution for the same problem.
Источник
Linux – Auto-Reboot and Shutdown with Cron Jobs – Task Scheduler alternative How to configure a daily or weekly automatic shutdown & reboot of your machine using cron job in Linux CentOS, Ubuntu, Debian, RHEL
Since we just published a guide to setup an auto-reboot or auto-shutdown on Windows machines, we thought that it could be useful to do the same for Linux systems as well.
The Linux built-in counterpart of the Windows Task Scheduler is called cron and it’s available on all the major distributions – CentOS, Ubuntu, Debian, RHEL and so on. As we can expect from Linux, instead of the fancy GUI provided by Windows we can set up our tasks with a simple and practical configuration file – which you will find in the following path:
The great thing about the crontab file is that it contains an awesome comment-based tutorial which will instruct you on everything at a glance:
As we can see from the last line of the above code snippet, we did setup a system auto-reboot on every sunday at 00:00 AM (midnight) using the /sbin/shutdown command, which is the exact counterpart of the Windows shutdown.exe tool.
Cron web-based GUI
If you’re looking for a fancier way of doing things, you can also setup your cron jobs with crontab.guru, a great web-based tool that allows to do that with a nice graphical interface. This tool can be very useful for beginners because it auto-calculates the timing options and translates them in a human-readable way, thus avoiding unwanted errors. Cron job failures can be disastrous, the website says in a tagline right below the GUI, and it’s definitely right!
Troubleshooting
Cron jobs are great, but they come with a minor flaw that can make the life hard for Linux newcomers: the cron service runs off the hardware clock rather than system clock, which can lead to minor or major disasters depending on your configuration and time zone settings, such as:
- Cron jobs running at incorrect time, usually UTC instead of your local timezone.
- Cron jobs running each and every minute – an edge-case scenario which happend to me some weeks ago, which I’ll describe in another post.
If you are experiencing this, check out your hardware clock by typing hwclock, then your system clock by typing date: if they do differ, you definitely have an out-of-sync issue which you should properly address.
The best thing you can do to fix that is to synchronize your hardware clock with your system clock using the following command:
Источник
crontab’s @reboot only works for root?
man 5 crontab is pretty clear on how to use crontab to run a script on boot:
So I happily added a single line to my crontab (under my user account, not root):
But for some reason, myscript.sh wouldn’t run on machine reboot. (it runs fine if I invoke it from the command line, so it’s not a permissions problem)
What am I missing?
Update to answer @Anthon’s questions:
- Oracle-linux version: 5.8 (uname: 2.6.32-300.39.2.el5uek #1 SMP)
- Cron version: vixie-cron-4.1-81.el5.x86_64
- Yes, /home is a mounted partition. Looks like this is the problem. How do I workaround this?
- Currently, myscript.sh only echos a text message to a file in /home/me .
9 Answers 9
This can be a bit of a confusing topic because there are different implementations of cron. Also there were several bugs that broke this feature, and there are also some use cases where it simply won’t work, specifically if you do a shutdown/boot vs. a reboot.
One such bug in Debian is covered here, titled: cron: @reboot jobs are not run. This seems to have made it’s way into Ubuntu as well, which I can’t confirm directly.
Evidence of the bug in Ubuntu would seem to be confirmed here in this SO Q&A titled: @reboot cronjob not executing.
comment #1: . 3) your version of crond may not support @reboot are you using vix’s crond? . show results of crontab -l -u user
comment #2: . It might be a good idea to set it up as an init script instead of relying on a specific version of cron’s @reboot.
comment #3: . @MarkRoberts removed the reboot and modified the 1 * * * * , to */1 * * * * , problem is solved! Where do I send the rep pts Mark? Thank you!
The accepted answer in that Q&A also had this comment:
Seems to me Lubuntu doesn’t support the @Reboot Cron syntax.
Additional evidence
As additional evidence there was this thread that someone was attempting the very same thing and getting frustrated that it didn’t work. It’s titled: Thread: Cron — @reboot jobs not working.
Re: Cron — @reboot jobs not working
Quote Originally Posted by ceallred View Post This is killing me. Tried the wrapper script. Running manually generates the log file. rebooting and the job doesn’t run or create log file.
Syslog shows that CRON ran the job. but again, no output and the process isn’t running. Jul 15 20:07:45 RavenWing cron[1026]: (CRON) INFO (Running @reboot jobs) Jul 15 20:07:45 RavenWing CRON[1053]: (ceallred) CMD (/home/ceallred/Scripts/run_spideroak.sh > /home/ceallred/Scripts/SpiderOak.log 2>&1 &)
It’s seems like cron doesn’t like the @reboot command. Any other ideas?
Okay. Partially solved. I’ll mark this one as solved and start a new thread with the new issue.
I think the answer was my encrypted home directory wasn’t mounted when CRON was trying to run the script (stored in /home/username/scripts). Moved to /usr/scripts and the job runs as expected.
So now it appears to be a spideroak issue. Process starts, but by the time the boot process is finished, it’s gone. I’m guessing a crash for some reason. New thread to ask about that.
Thanks for all the help!
Once this above user figured out his issue he was able to get @reboot working out of the crontab entry of a user.
I’m not entirely sure what version of cron is used on Ubuntu, but this would seem to indicate that user’s can use @reboot too, or that the bug was fixed at some point in subsequent versions of cron.
I tested on CentOS 6 the following and it worked.
Example
I then rebooted the system.
After the reboot.
Take aways
- This feature does seem to be supported for both system and user crontab entries.
- You have to make sure that it’s supported/working in your particular distro and/or version of the cron package.
For more on how the actual mechanism works for @reboot I did come across this blog post which discusses the innards. It’s titled: @reboot — explaining simple cron magic.
Debugging crond
You can turn up the verbosity of crond by adding the following to this configuration file on RHEL/CentOS/Fedora based distros.
The valid levels are 0, 1, or 2. To revert this file back to it’s default logging level simply remove the «-L 2» when you’re done debugging the situation.
Источник