2

I was currently controlling this through an uptime. The computer restarts if uptime is greater than 1h.

But I do not know how to control if the computer is one day on or more, because currently I only control the hours.

Is it possible to control days, hours and minutes with uptime? I need to restart the computer when the power on time is greater than 1h. If the time is 1 day and 0 hours gives failure.

Sorry for my explanation, it is a script that does a series of things and alfinal exists this function that is responsible for controlling this parameter.

thanks for reading me

  • 1
    I'm voting to close this question as off-topic because it is not about programming, but about Linux usage. I suggest to re-ask this question on https://unix.stackexchange.com . – peterh Oct 01 '18 at 00:42

2 Answers2

2

Not sure I quite understand your issue.

If you want your computer to ALWAYS reboot after a specific amount of time, which is very unusual, then use cron. Add this to /etc/crontab (alternatively, if there is a /etc/cron.d directory on your machine, you can also create a file /etc/cron.d/reboot with this content) :

@reboot    root    sleep 1800; /sbin/reboot

(adapt reboot's path to match your system; 1800 is the number of seconds for 30 minutes, change it to whatever delay you need)


On the other hand, you may be writing a script that will reboot your server, and you may want to keep it from working if it is run before 30 minutes of uptime (which makes more sense).

Then, I understand you have difficulties parsing the result of uptime and you should use /proc/uptime which gives your uptime in seconds:

#!/bin/sh
not_before=1800             # Number of seconds - adapt to your needs
uptime=$(cut -d . -f 1 /proc/uptime)

[ "$uptime" -ge "$not_before" ] && exec reboot
echo "Sorry, only $uptime s of uptime; you must wait $((not_before - uptime)) seconds" >&2
exit 1
xhienne
  • 5,738
  • 1
  • 15
  • 34
1

If you want to do it in a script, use the result of uptime | grep " day"to determine whether to execute things (in anifcondition), then do anything you want inside theif`.

Make that script executable and put it in crontab to run every 5min or so.

More information on Cron: https://wiki.archlinux.org/index.php/Cron

LuRsT
  • 3,973
  • 9
  • 39
  • 51
  • Sorry for my explanation, it is a script that does a series of things and alfinal exists this function that is responsible for controlling this parameter. – Ruben Serena Tellechea Sep 30 '18 at 18:29
  • No worries @RubenSerenaTellechea, could you please edit your question? I updated my answer with a solution to the comment :) – LuRsT Sep 30 '18 at 18:33
  • 1
    AFAIK `60 * * * *` is not a valid cron entry. Minutes must be 0 to 59. Moreover, if your server as rebooted at 12h45, your cron will trigger a reboot 15 minutes later, i.e. it's not going to wait 60 minutes. – xhienne Sep 30 '18 at 21:57
  • @xhienne oh, so it would run only at that minute in the crontab :(, you're right, my first solution wouldn't work, I'll update it to remove that first part – LuRsT Oct 01 '18 at 06:32