4

I want to reboot my device ,which has Openwrt os .I want the command run using crontab and execute only once.

Munmun Chowdhury
  • 41
  • 1
  • 1
  • 4
  • `cron` is for *repeatedly* executing a command at some period or schedule. If what you're really after is truly a single execution, you *can* use `cron` (covered by parlor tricks like deleting the script as described below), but (unless you're rebooting into another instance entirely) `cron` will continue to attempt to execute that command at that interval. Granted, you can make the date/time so specific as to execute seldomly, but it's still on the books waiting to be re-executed. – posita Jul 15 '23 at 23:09

5 Answers5

4

If you want to run only once then better ssh the openwrt and go the terminal and type reboot. Which will reboot the system.

In case if you want to runt the reboot at specific time then you can use cronjob. Which has the following syntax

*     *     *   *    *        command to be executed
-     -     -   -    -
|     |     |   |    |
|     |     |   |    +----- day of week (0 - 6) (Sunday=0)
|     |     |   +------- month (1 - 12)
|     |     +--------- day of        month (1 - 31)
|     +----------- hour (0 - 23)
+------------- min (0 - 59)

Use the following command in your openwrt command line to open crontab

crontab -e //-e stands for edit

Now based on the above syntax you can tell when to run the script

Eg : To run on January 1st of every year you can write the following command

* * 1 1 * reboot

Save the file and it will do the trick.

Channaveer Hakari
  • 2,769
  • 3
  • 34
  • 45
  • See the quoted passage in my answer as to why a naked reboot statement in a cron job is potentially unsafe in OpenWRT and could result in an endless cycle of reboots. – posita Jan 24 '23 at 12:17
  • Also, be careful about providing values for both day-of-month and day-of-week fields. From the [docs](https://www.man7.org/linux/man-pages/man5/crontab.5.html): *Note: The day of a command's execution can be specified by two fields — day of month, and day of week. If both fields are restricted (i.e., don't start with `*`), the command will be run when either field matches the current time.* – posita Jul 15 '23 at 23:13
0

I am assuming by saying only once, you meant only once in a day? If that is the case then do the following

crontab -e

Add the following line there and save the file.

15 3 * * * reboot

Now reboot the cron service

/etc/init.d/cron restart

It will reboot the router everyday at 03:15 AM.

  • Hi,I meant only once for that device .Not once per day – Munmun Chowdhury Oct 06 '17 at 09:22
  • There have many ways. Use what fits you. Like you can use self deleting script. The script will execute your commands and delete itself in the end. Use rm — “$0” at the end of your script. It will delete itself. Another way, you can also create a file after executing commands and then check if that file exists. If so then exit the script. – Abdullah Al Farooq Oct 06 '17 at 09:35
  • See the quoted passage in my answer as to why a naked reboot statement in a cron job is potentially unsafe in OpenWRT and could result in an endless cycle of reboots. – posita Jan 24 '23 at 12:16
0

reboot at 3:40 every day

40 3 * * * sleep 70 && touch /etc/banner && reboot

; from wiki: https://wiki.openwrt.org/doc/howto/cron

Wangwang
  • 157
  • 2
-1

Openwrt crontab does not support @reboot, so you can use once a minute command:

*****

and then check in your script to be executed if there is no instance of it running on the shell.

But, you also can use rc.local and put your script on it. Just don't forget to make rc.local executable permission, sometimes it needs changing permissions.

Another option is also running in /etc/init.d/ if it is a service.

Majid
  • 49
  • 4
-1

Don't use cron, use sleep

EDIT: Apparently, the OP intended to automate a single reboot at some later date. If that's indeed the case, then cron probably isn't the best tool. The first alternative I'd consider is sleep:

sleep 300 && reboot  # wait 5 min (300 sec), then reboot

The && requires the sleep command to exit successfully in order to move onto the next one (which is helpful if you want to interrupt sleep without triggering the reboot). See this answer if you need that command to run without an interactive terminal.

If you want the reboot to happen at a particular time, you can use date to compute the sleep argument:

# reboot at 15:30 today
sleep $((
    $( date +%s -d 15:30 )
  - $( date +%s          )
)) && reboot

The term date +%s -d 15:30 will give the UNIX timestamp for 15:30 today, and the term date +%s will give the UNIX timestamp for right now.

You can perform additional computations as well:

# reboot at 15:30 tomorrow
sleep $((
    $( date +%s -d 15:30 )
  - $( date +%s          )
  + 24 * 60 * 60
)) && reboot
# reboot on Feb 3 of this year at 15:30
sleep $((
    $( date +%s -d '02.03-15:30' )
  - $( date +%s          )
  + 24 * 60 * 60
)) && reboot
# reboot on 2023-02-02 at 15:30
sleep $((
    $( date +%s -d '2023-02-02 15:30' )
  - $( date +%s          )
  + 24 * 60 * 60
)) && reboot

date accepts several different formats for its -d argument:

# date --help
...
Recognized TIME formats:
        @seconds_since_1970
        hh:mm[:ss]
        [YYYY.]MM.DD-hh:mm[:ss]
        YYYY-MM-DD hh:mm[:ss]
        [[[[[YY]YY]MM]DD]hh]mm[.ss]

My original answer is preserved below for others coming to this question thinking it is about automating periodic reboots instead of a single one.


cron is for periodic jobs, but reboot has special constraints on OpenWRT

It's not clear from the original question, but rebooting OpenWRT via cron can be tricky, and can sometimes result in an infinite loop (which may have been what the OP was trying to avoid). The good news is that there's an explanation and a relatively simple work-around provided in OpenWRT's docs:

In the boot process the clock is initially set by sysfixtime to the most recent timestamp of any file found in /etc. The most recent file is possibly a status file or config file, modified maybe 30 seconds before the reboot initiated by cron. So, in the boot process the clock gets set backwards a few seconds to that file's timestamp. Then cron starts and notices a few seconds later that the required boot moment has again arrived and reboots again… At the end of the boot process ntpd starts, and it may also take a while before ntpd gets and sets the correct time, so cron may start the reboot in between.

One solution for cron is to use a delay and touch a file in /etc before reboot.

# Reboot at 4:30am every day
# Note: To avoid infinite reboot loop, wait 70 seconds
# and touch a file in /etc so clock will be set
# properly to 4:31 on reboot before cron starts.
30 4 * * * sleep 70 && touch /etc/banner && reboot
posita
  • 864
  • 1
  • 9
  • 17