Skip to content
Advertisement

Restart the computer when the power on time exceeds 30 minutes [closed]

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

Advertisement

Answer

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
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement