How to shut down linux server after running for 60 minutes
Clash Royale CLAN TAG#URR8PPP
up vote
8
down vote
favorite
I have a server that it is normally switched off for security reasons. When I want to work on it I switch it on, execute my tasks, then shutdown it again. My tasks take usually no more than 15 minutes. I would like to implement a mechanism to shutdown it automatically after 60 minutes.
I've researched how to do this with cron, but I don't think it's the proper way because cron doesn't take into account when the server was last turned on, I can only set periodic patterns but they don't take that data into account.
How could I do this implementation?
linux cron date shutdown
New contributor
add a comment |Â
up vote
8
down vote
favorite
I have a server that it is normally switched off for security reasons. When I want to work on it I switch it on, execute my tasks, then shutdown it again. My tasks take usually no more than 15 minutes. I would like to implement a mechanism to shutdown it automatically after 60 minutes.
I've researched how to do this with cron, but I don't think it's the proper way because cron doesn't take into account when the server was last turned on, I can only set periodic patterns but they don't take that data into account.
How could I do this implementation?
linux cron date shutdown
New contributor
2
Have a look atat
(one-time execution).
â dirkt
13 hours ago
3
I haven't done this, but your login script could start asudo shutdown -h +60
which would start a countdown counter of 60 mins for the shutdown (-halt) process. If you wanted to cancel it, you couldsudo shutdown -c
(this doesn't use cron though)
â guiverc
13 hours ago
I might have a solution that will shut it down as soon as your task is done. May I know how you run these tasks, by a bash script?
â sdkks
11 hours ago
5
Depending on the nature of your tasks maybe it's worth running them inside a docker container, so you don't need to worry about switching off a server. The container is created, it runs your tasks, and it's destroyed after that.
â Vicente Olivert Riera
11 hours ago
add a comment |Â
up vote
8
down vote
favorite
up vote
8
down vote
favorite
I have a server that it is normally switched off for security reasons. When I want to work on it I switch it on, execute my tasks, then shutdown it again. My tasks take usually no more than 15 minutes. I would like to implement a mechanism to shutdown it automatically after 60 minutes.
I've researched how to do this with cron, but I don't think it's the proper way because cron doesn't take into account when the server was last turned on, I can only set periodic patterns but they don't take that data into account.
How could I do this implementation?
linux cron date shutdown
New contributor
I have a server that it is normally switched off for security reasons. When I want to work on it I switch it on, execute my tasks, then shutdown it again. My tasks take usually no more than 15 minutes. I would like to implement a mechanism to shutdown it automatically after 60 minutes.
I've researched how to do this with cron, but I don't think it's the proper way because cron doesn't take into account when the server was last turned on, I can only set periodic patterns but they don't take that data into account.
How could I do this implementation?
linux cron date shutdown
linux cron date shutdown
New contributor
New contributor
edited 13 mins ago
Malandy
1032
1032
New contributor
asked 13 hours ago
jmhostalet
1464
1464
New contributor
New contributor
2
Have a look atat
(one-time execution).
â dirkt
13 hours ago
3
I haven't done this, but your login script could start asudo shutdown -h +60
which would start a countdown counter of 60 mins for the shutdown (-halt) process. If you wanted to cancel it, you couldsudo shutdown -c
(this doesn't use cron though)
â guiverc
13 hours ago
I might have a solution that will shut it down as soon as your task is done. May I know how you run these tasks, by a bash script?
â sdkks
11 hours ago
5
Depending on the nature of your tasks maybe it's worth running them inside a docker container, so you don't need to worry about switching off a server. The container is created, it runs your tasks, and it's destroyed after that.
â Vicente Olivert Riera
11 hours ago
add a comment |Â
2
Have a look atat
(one-time execution).
â dirkt
13 hours ago
3
I haven't done this, but your login script could start asudo shutdown -h +60
which would start a countdown counter of 60 mins for the shutdown (-halt) process. If you wanted to cancel it, you couldsudo shutdown -c
(this doesn't use cron though)
â guiverc
13 hours ago
I might have a solution that will shut it down as soon as your task is done. May I know how you run these tasks, by a bash script?
â sdkks
11 hours ago
5
Depending on the nature of your tasks maybe it's worth running them inside a docker container, so you don't need to worry about switching off a server. The container is created, it runs your tasks, and it's destroyed after that.
â Vicente Olivert Riera
11 hours ago
2
2
Have a look at
at
(one-time execution).â dirkt
13 hours ago
Have a look at
at
(one-time execution).â dirkt
13 hours ago
3
3
I haven't done this, but your login script could start a
sudo shutdown -h +60
which would start a countdown counter of 60 mins for the shutdown (-halt) process. If you wanted to cancel it, you could sudo shutdown -c
(this doesn't use cron though)â guiverc
13 hours ago
I haven't done this, but your login script could start a
sudo shutdown -h +60
which would start a countdown counter of 60 mins for the shutdown (-halt) process. If you wanted to cancel it, you could sudo shutdown -c
(this doesn't use cron though)â guiverc
13 hours ago
I might have a solution that will shut it down as soon as your task is done. May I know how you run these tasks, by a bash script?
â sdkks
11 hours ago
I might have a solution that will shut it down as soon as your task is done. May I know how you run these tasks, by a bash script?
â sdkks
11 hours ago
5
5
Depending on the nature of your tasks maybe it's worth running them inside a docker container, so you don't need to worry about switching off a server. The container is created, it runs your tasks, and it's destroyed after that.
â Vicente Olivert Riera
11 hours ago
Depending on the nature of your tasks maybe it's worth running them inside a docker container, so you don't need to worry about switching off a server. The container is created, it runs your tasks, and it's destroyed after that.
â Vicente Olivert Riera
11 hours ago
add a comment |Â
7 Answers
7
active
oldest
votes
up vote
12
down vote
accepted
If you execute your tasks as the same user every time, you can simply add the shutdown command with the option -P to your profile. The number stands for the amount of seconds the shutdown command is delayed.
echo "sudo shutdown -P 3600" >> ~/.profile
2
The time is in minutes, not seconds. Tryshutdown -k -P 3600
vsshutdown -k -P 60
(-k
prints wall message but doesn't have other effect)
â sebasth
11 hours ago
5
But try the command one time first, you don't want an instant shutdown to be bound to your profile!
â Fabian Röling
10 hours ago
6
I imagine every ssh login would cause a new call of shutdown. Would the counter reset then?
â JoL
8 hours ago
1
It might also be worthwhile to mention that logins will not be allowed in the last 5 minutes, as mentioned in the shutdown manpage.
â JoL
8 hours ago
add a comment |Â
up vote
32
down vote
There are a several options.
Provide time directly to
shutdown -P
:shutdown -P 60
Note that
shutdown
man page also points out:
If the time argument is used, 5 minutes before the system goes down the /run/nologin file is created to ensure that further logins shall not be allowed.
Use
at
command.Create a systemd unit file or init script which runs
shutdown -P 60
at startup.Use cron's
@reboot
to run command after boot.Add to (root) crontab:
@reboot shutdown -P 60
For the last two methods, you could also use sleep 3600 && shutdown -P
instead of using time argument to shutdown
to delay the shutdown for 60 minutes. This way logins are possible to the last moment before shutdown is issued.
add a comment |Â
up vote
6
down vote
Hello and welcome to this site!
Elaborating on @dirkt comment, you can insert an at
command on your .bashrc
or .profile
or whichever file your shell uses on login to schedule an automatic shutdown 60 minutes after your login.
Something like:
at now + 60 minutes -f /sbin/halt
This task is right upat
âÂÂs alley. You can also seeat
tasks in/var/spool/
â sdkks
11 hours ago
2
I would advice against usingat
in this context because it survives reboots. If OP were to log in, manually shutdown and then log in again in the span of an hour, he'd get kicked off before an hour passed after his last login when the first scheduled shutdown proc.
â Aaron
10 hours ago
@Aaron thatâÂÂs true. HavenâÂÂt thought of that. Boot persistence is a caveat for this solution.
â sdkks
10 hours ago
add a comment |Â
up vote
4
down vote
This looks like an XY problem.
My tasks take usually no more than 15 minutes. I would like to implement a mechanism to shutdown it automatically after 60 minutes.
If you shutdown after 60 minutes, you run the risk that you may be running a particularly complicated problem and just need to have more time. Many of the previous solutions would not make it easy to delay the shutdown.
If the task is not an interactive task but instead are a scripted task that is automatically triggered from another machine, @sdkks gave a great solution for that; you really should just task the machine to run poweroff as soon as the script and all its tasks finishes.
However, if your task is an interactive task, I'd suggest is to do idle detection instead.
If you do your task in a GUI (X11) you can detect the idle GUI sessions using the approach described here: Run a command when system is idle and when is active again
If you do the task through terminal, you can detect logged in user using the who
command. You can setup a cronjob that shuts the machine down if who
returns empty result. Note that this will be quite conservative approach, it will not shutdown the system if you left the console connected but idle.
If you want to be a bit more aggressive and disconnect idle terminal session as well, you can combine the previous approach with automatically disconnecting idle SSH session ClientAliveInterval
and ClientAliveCountMax
. Another approach for this if you don't have SSH but have a local terminal session is to use the terminal IDLE time as returned by w
command.
This is a very important point. Whatever you implement - it's in your interest to make sure that you can also cancel the shut down.
â Shadow
23 mins ago
add a comment |Â
up vote
3
down vote
First of all, welcome.
Although present answers here all satisfy the requirement to the perfection, I have a humble unsolicited suggestion, that's if you'd like your machine to power off as soon as the tasks are done.
bash
scripts can be trap
ped, meaning certain signals can be intercepted and certain tasks can be executed when needed. EXIT
is one of the signals that can be trapped.
You would be able to:
- Set a
trap
forEXIT
of your automated shell scripts, meaning
termination of your automated tasks - Set a
trap
for your.bashrc
EXIT
, meaning whenever you log out
of that machine, power it off.
Option #1 would be the ideal case, provided your tasks don't require adhoc inspection and manual judgement.
Option #2 would cover the cases where you'd forget to exit the terminal without powering off. There is a caveat though, if you have multiple terminals to same machine open and you exit from one of them, it will still power off the machine all the same. (It can be scripted to avoid that, but I won't complicate the solution.)
cleanup()
# Do some tasks before terminating
echo oh la lah, cleaning is so nice
echo "Cy'a later world"
sudo poweroff & # finally shutdown
trap cleanup EXIT
This can be at the end of .bashrc
for Option #2, at somewhere top of your script for Option #1.
Why not use poweroff
at the end of the script?
I prefer to use set -eo pipefail
in on top of my scripts, if any error happens, it won't silently fail, it will stop executing more commands. trap
of EXIT
signal should cover the cases where script terminates prematurely due to errors.
However for your tasks, this might also mean machine will shutdown before they are completed.
I have a simple bash
template I use for making scripting easier to debug, maybe might be of some use. Please see this gist.
add a comment |Â
up vote
2
down vote
Try to put the script (sudo shutdown -P 3600) in the /etc/init.d directory to run it automatically at startup.
Or try using anacron, adding the command in the /etc/anacrontab file.
I also suggest to use nohup in front of the command to be sure that the command still working after logout
New contributor
Depending on distro, it might not get executed by just being in init.d
â sdkks
11 hours ago
@sdkks, you're right. Anacron can be also considered. putting the script in /etc/anacrontab, also used with 'nohup' in the event of logout.
â Saveriofr
11 hours ago
@Saveriofr You can edit your answer to improve its quality (better than posting additional info as a comment).
â Anthony Geoghegan
11 hours ago
add a comment |Â
up vote
1
down vote
If you're really concerned about security, use an mechanical outlet timer on the power source. Just set it to whatever time you want it to shutdown. This way no one can login remotely and disable the shutdown. You would need physical access.
add a comment |Â
7 Answers
7
active
oldest
votes
7 Answers
7
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
12
down vote
accepted
If you execute your tasks as the same user every time, you can simply add the shutdown command with the option -P to your profile. The number stands for the amount of seconds the shutdown command is delayed.
echo "sudo shutdown -P 3600" >> ~/.profile
2
The time is in minutes, not seconds. Tryshutdown -k -P 3600
vsshutdown -k -P 60
(-k
prints wall message but doesn't have other effect)
â sebasth
11 hours ago
5
But try the command one time first, you don't want an instant shutdown to be bound to your profile!
â Fabian Röling
10 hours ago
6
I imagine every ssh login would cause a new call of shutdown. Would the counter reset then?
â JoL
8 hours ago
1
It might also be worthwhile to mention that logins will not be allowed in the last 5 minutes, as mentioned in the shutdown manpage.
â JoL
8 hours ago
add a comment |Â
up vote
12
down vote
accepted
If you execute your tasks as the same user every time, you can simply add the shutdown command with the option -P to your profile. The number stands for the amount of seconds the shutdown command is delayed.
echo "sudo shutdown -P 3600" >> ~/.profile
2
The time is in minutes, not seconds. Tryshutdown -k -P 3600
vsshutdown -k -P 60
(-k
prints wall message but doesn't have other effect)
â sebasth
11 hours ago
5
But try the command one time first, you don't want an instant shutdown to be bound to your profile!
â Fabian Röling
10 hours ago
6
I imagine every ssh login would cause a new call of shutdown. Would the counter reset then?
â JoL
8 hours ago
1
It might also be worthwhile to mention that logins will not be allowed in the last 5 minutes, as mentioned in the shutdown manpage.
â JoL
8 hours ago
add a comment |Â
up vote
12
down vote
accepted
up vote
12
down vote
accepted
If you execute your tasks as the same user every time, you can simply add the shutdown command with the option -P to your profile. The number stands for the amount of seconds the shutdown command is delayed.
echo "sudo shutdown -P 3600" >> ~/.profile
If you execute your tasks as the same user every time, you can simply add the shutdown command with the option -P to your profile. The number stands for the amount of seconds the shutdown command is delayed.
echo "sudo shutdown -P 3600" >> ~/.profile
answered 13 hours ago
Dirk Krijgsman
1844
1844
2
The time is in minutes, not seconds. Tryshutdown -k -P 3600
vsshutdown -k -P 60
(-k
prints wall message but doesn't have other effect)
â sebasth
11 hours ago
5
But try the command one time first, you don't want an instant shutdown to be bound to your profile!
â Fabian Röling
10 hours ago
6
I imagine every ssh login would cause a new call of shutdown. Would the counter reset then?
â JoL
8 hours ago
1
It might also be worthwhile to mention that logins will not be allowed in the last 5 minutes, as mentioned in the shutdown manpage.
â JoL
8 hours ago
add a comment |Â
2
The time is in minutes, not seconds. Tryshutdown -k -P 3600
vsshutdown -k -P 60
(-k
prints wall message but doesn't have other effect)
â sebasth
11 hours ago
5
But try the command one time first, you don't want an instant shutdown to be bound to your profile!
â Fabian Röling
10 hours ago
6
I imagine every ssh login would cause a new call of shutdown. Would the counter reset then?
â JoL
8 hours ago
1
It might also be worthwhile to mention that logins will not be allowed in the last 5 minutes, as mentioned in the shutdown manpage.
â JoL
8 hours ago
2
2
The time is in minutes, not seconds. Try
shutdown -k -P 3600
vs shutdown -k -P 60
(-k
prints wall message but doesn't have other effect)â sebasth
11 hours ago
The time is in minutes, not seconds. Try
shutdown -k -P 3600
vs shutdown -k -P 60
(-k
prints wall message but doesn't have other effect)â sebasth
11 hours ago
5
5
But try the command one time first, you don't want an instant shutdown to be bound to your profile!
â Fabian Röling
10 hours ago
But try the command one time first, you don't want an instant shutdown to be bound to your profile!
â Fabian Röling
10 hours ago
6
6
I imagine every ssh login would cause a new call of shutdown. Would the counter reset then?
â JoL
8 hours ago
I imagine every ssh login would cause a new call of shutdown. Would the counter reset then?
â JoL
8 hours ago
1
1
It might also be worthwhile to mention that logins will not be allowed in the last 5 minutes, as mentioned in the shutdown manpage.
â JoL
8 hours ago
It might also be worthwhile to mention that logins will not be allowed in the last 5 minutes, as mentioned in the shutdown manpage.
â JoL
8 hours ago
add a comment |Â
up vote
32
down vote
There are a several options.
Provide time directly to
shutdown -P
:shutdown -P 60
Note that
shutdown
man page also points out:
If the time argument is used, 5 minutes before the system goes down the /run/nologin file is created to ensure that further logins shall not be allowed.
Use
at
command.Create a systemd unit file or init script which runs
shutdown -P 60
at startup.Use cron's
@reboot
to run command after boot.Add to (root) crontab:
@reboot shutdown -P 60
For the last two methods, you could also use sleep 3600 && shutdown -P
instead of using time argument to shutdown
to delay the shutdown for 60 minutes. This way logins are possible to the last moment before shutdown is issued.
add a comment |Â
up vote
32
down vote
There are a several options.
Provide time directly to
shutdown -P
:shutdown -P 60
Note that
shutdown
man page also points out:
If the time argument is used, 5 minutes before the system goes down the /run/nologin file is created to ensure that further logins shall not be allowed.
Use
at
command.Create a systemd unit file or init script which runs
shutdown -P 60
at startup.Use cron's
@reboot
to run command after boot.Add to (root) crontab:
@reboot shutdown -P 60
For the last two methods, you could also use sleep 3600 && shutdown -P
instead of using time argument to shutdown
to delay the shutdown for 60 minutes. This way logins are possible to the last moment before shutdown is issued.
add a comment |Â
up vote
32
down vote
up vote
32
down vote
There are a several options.
Provide time directly to
shutdown -P
:shutdown -P 60
Note that
shutdown
man page also points out:
If the time argument is used, 5 minutes before the system goes down the /run/nologin file is created to ensure that further logins shall not be allowed.
Use
at
command.Create a systemd unit file or init script which runs
shutdown -P 60
at startup.Use cron's
@reboot
to run command after boot.Add to (root) crontab:
@reboot shutdown -P 60
For the last two methods, you could also use sleep 3600 && shutdown -P
instead of using time argument to shutdown
to delay the shutdown for 60 minutes. This way logins are possible to the last moment before shutdown is issued.
There are a several options.
Provide time directly to
shutdown -P
:shutdown -P 60
Note that
shutdown
man page also points out:
If the time argument is used, 5 minutes before the system goes down the /run/nologin file is created to ensure that further logins shall not be allowed.
Use
at
command.Create a systemd unit file or init script which runs
shutdown -P 60
at startup.Use cron's
@reboot
to run command after boot.Add to (root) crontab:
@reboot shutdown -P 60
For the last two methods, you could also use sleep 3600 && shutdown -P
instead of using time argument to shutdown
to delay the shutdown for 60 minutes. This way logins are possible to the last moment before shutdown is issued.
edited 7 hours ago
answered 13 hours ago
sebasth
6,80131745
6,80131745
add a comment |Â
add a comment |Â
up vote
6
down vote
Hello and welcome to this site!
Elaborating on @dirkt comment, you can insert an at
command on your .bashrc
or .profile
or whichever file your shell uses on login to schedule an automatic shutdown 60 minutes after your login.
Something like:
at now + 60 minutes -f /sbin/halt
This task is right upat
âÂÂs alley. You can also seeat
tasks in/var/spool/
â sdkks
11 hours ago
2
I would advice against usingat
in this context because it survives reboots. If OP were to log in, manually shutdown and then log in again in the span of an hour, he'd get kicked off before an hour passed after his last login when the first scheduled shutdown proc.
â Aaron
10 hours ago
@Aaron thatâÂÂs true. HavenâÂÂt thought of that. Boot persistence is a caveat for this solution.
â sdkks
10 hours ago
add a comment |Â
up vote
6
down vote
Hello and welcome to this site!
Elaborating on @dirkt comment, you can insert an at
command on your .bashrc
or .profile
or whichever file your shell uses on login to schedule an automatic shutdown 60 minutes after your login.
Something like:
at now + 60 minutes -f /sbin/halt
This task is right upat
âÂÂs alley. You can also seeat
tasks in/var/spool/
â sdkks
11 hours ago
2
I would advice against usingat
in this context because it survives reboots. If OP were to log in, manually shutdown and then log in again in the span of an hour, he'd get kicked off before an hour passed after his last login when the first scheduled shutdown proc.
â Aaron
10 hours ago
@Aaron thatâÂÂs true. HavenâÂÂt thought of that. Boot persistence is a caveat for this solution.
â sdkks
10 hours ago
add a comment |Â
up vote
6
down vote
up vote
6
down vote
Hello and welcome to this site!
Elaborating on @dirkt comment, you can insert an at
command on your .bashrc
or .profile
or whichever file your shell uses on login to schedule an automatic shutdown 60 minutes after your login.
Something like:
at now + 60 minutes -f /sbin/halt
Hello and welcome to this site!
Elaborating on @dirkt comment, you can insert an at
command on your .bashrc
or .profile
or whichever file your shell uses on login to schedule an automatic shutdown 60 minutes after your login.
Something like:
at now + 60 minutes -f /sbin/halt
answered 13 hours ago
Mr Shunz
2,76811720
2,76811720
This task is right upat
âÂÂs alley. You can also seeat
tasks in/var/spool/
â sdkks
11 hours ago
2
I would advice against usingat
in this context because it survives reboots. If OP were to log in, manually shutdown and then log in again in the span of an hour, he'd get kicked off before an hour passed after his last login when the first scheduled shutdown proc.
â Aaron
10 hours ago
@Aaron thatâÂÂs true. HavenâÂÂt thought of that. Boot persistence is a caveat for this solution.
â sdkks
10 hours ago
add a comment |Â
This task is right upat
âÂÂs alley. You can also seeat
tasks in/var/spool/
â sdkks
11 hours ago
2
I would advice against usingat
in this context because it survives reboots. If OP were to log in, manually shutdown and then log in again in the span of an hour, he'd get kicked off before an hour passed after his last login when the first scheduled shutdown proc.
â Aaron
10 hours ago
@Aaron thatâÂÂs true. HavenâÂÂt thought of that. Boot persistence is a caveat for this solution.
â sdkks
10 hours ago
This task is right up
at
âÂÂs alley. You can also see at
tasks in /var/spool/
â sdkks
11 hours ago
This task is right up
at
âÂÂs alley. You can also see at
tasks in /var/spool/
â sdkks
11 hours ago
2
2
I would advice against using
at
in this context because it survives reboots. If OP were to log in, manually shutdown and then log in again in the span of an hour, he'd get kicked off before an hour passed after his last login when the first scheduled shutdown proc.â Aaron
10 hours ago
I would advice against using
at
in this context because it survives reboots. If OP were to log in, manually shutdown and then log in again in the span of an hour, he'd get kicked off before an hour passed after his last login when the first scheduled shutdown proc.â Aaron
10 hours ago
@Aaron thatâÂÂs true. HavenâÂÂt thought of that. Boot persistence is a caveat for this solution.
â sdkks
10 hours ago
@Aaron thatâÂÂs true. HavenâÂÂt thought of that. Boot persistence is a caveat for this solution.
â sdkks
10 hours ago
add a comment |Â
up vote
4
down vote
This looks like an XY problem.
My tasks take usually no more than 15 minutes. I would like to implement a mechanism to shutdown it automatically after 60 minutes.
If you shutdown after 60 minutes, you run the risk that you may be running a particularly complicated problem and just need to have more time. Many of the previous solutions would not make it easy to delay the shutdown.
If the task is not an interactive task but instead are a scripted task that is automatically triggered from another machine, @sdkks gave a great solution for that; you really should just task the machine to run poweroff as soon as the script and all its tasks finishes.
However, if your task is an interactive task, I'd suggest is to do idle detection instead.
If you do your task in a GUI (X11) you can detect the idle GUI sessions using the approach described here: Run a command when system is idle and when is active again
If you do the task through terminal, you can detect logged in user using the who
command. You can setup a cronjob that shuts the machine down if who
returns empty result. Note that this will be quite conservative approach, it will not shutdown the system if you left the console connected but idle.
If you want to be a bit more aggressive and disconnect idle terminal session as well, you can combine the previous approach with automatically disconnecting idle SSH session ClientAliveInterval
and ClientAliveCountMax
. Another approach for this if you don't have SSH but have a local terminal session is to use the terminal IDLE time as returned by w
command.
This is a very important point. Whatever you implement - it's in your interest to make sure that you can also cancel the shut down.
â Shadow
23 mins ago
add a comment |Â
up vote
4
down vote
This looks like an XY problem.
My tasks take usually no more than 15 minutes. I would like to implement a mechanism to shutdown it automatically after 60 minutes.
If you shutdown after 60 minutes, you run the risk that you may be running a particularly complicated problem and just need to have more time. Many of the previous solutions would not make it easy to delay the shutdown.
If the task is not an interactive task but instead are a scripted task that is automatically triggered from another machine, @sdkks gave a great solution for that; you really should just task the machine to run poweroff as soon as the script and all its tasks finishes.
However, if your task is an interactive task, I'd suggest is to do idle detection instead.
If you do your task in a GUI (X11) you can detect the idle GUI sessions using the approach described here: Run a command when system is idle and when is active again
If you do the task through terminal, you can detect logged in user using the who
command. You can setup a cronjob that shuts the machine down if who
returns empty result. Note that this will be quite conservative approach, it will not shutdown the system if you left the console connected but idle.
If you want to be a bit more aggressive and disconnect idle terminal session as well, you can combine the previous approach with automatically disconnecting idle SSH session ClientAliveInterval
and ClientAliveCountMax
. Another approach for this if you don't have SSH but have a local terminal session is to use the terminal IDLE time as returned by w
command.
This is a very important point. Whatever you implement - it's in your interest to make sure that you can also cancel the shut down.
â Shadow
23 mins ago
add a comment |Â
up vote
4
down vote
up vote
4
down vote
This looks like an XY problem.
My tasks take usually no more than 15 minutes. I would like to implement a mechanism to shutdown it automatically after 60 minutes.
If you shutdown after 60 minutes, you run the risk that you may be running a particularly complicated problem and just need to have more time. Many of the previous solutions would not make it easy to delay the shutdown.
If the task is not an interactive task but instead are a scripted task that is automatically triggered from another machine, @sdkks gave a great solution for that; you really should just task the machine to run poweroff as soon as the script and all its tasks finishes.
However, if your task is an interactive task, I'd suggest is to do idle detection instead.
If you do your task in a GUI (X11) you can detect the idle GUI sessions using the approach described here: Run a command when system is idle and when is active again
If you do the task through terminal, you can detect logged in user using the who
command. You can setup a cronjob that shuts the machine down if who
returns empty result. Note that this will be quite conservative approach, it will not shutdown the system if you left the console connected but idle.
If you want to be a bit more aggressive and disconnect idle terminal session as well, you can combine the previous approach with automatically disconnecting idle SSH session ClientAliveInterval
and ClientAliveCountMax
. Another approach for this if you don't have SSH but have a local terminal session is to use the terminal IDLE time as returned by w
command.
This looks like an XY problem.
My tasks take usually no more than 15 minutes. I would like to implement a mechanism to shutdown it automatically after 60 minutes.
If you shutdown after 60 minutes, you run the risk that you may be running a particularly complicated problem and just need to have more time. Many of the previous solutions would not make it easy to delay the shutdown.
If the task is not an interactive task but instead are a scripted task that is automatically triggered from another machine, @sdkks gave a great solution for that; you really should just task the machine to run poweroff as soon as the script and all its tasks finishes.
However, if your task is an interactive task, I'd suggest is to do idle detection instead.
If you do your task in a GUI (X11) you can detect the idle GUI sessions using the approach described here: Run a command when system is idle and when is active again
If you do the task through terminal, you can detect logged in user using the who
command. You can setup a cronjob that shuts the machine down if who
returns empty result. Note that this will be quite conservative approach, it will not shutdown the system if you left the console connected but idle.
If you want to be a bit more aggressive and disconnect idle terminal session as well, you can combine the previous approach with automatically disconnecting idle SSH session ClientAliveInterval
and ClientAliveCountMax
. Another approach for this if you don't have SSH but have a local terminal session is to use the terminal IDLE time as returned by w
command.
answered 8 hours ago
Lie Ryan
66337
66337
This is a very important point. Whatever you implement - it's in your interest to make sure that you can also cancel the shut down.
â Shadow
23 mins ago
add a comment |Â
This is a very important point. Whatever you implement - it's in your interest to make sure that you can also cancel the shut down.
â Shadow
23 mins ago
This is a very important point. Whatever you implement - it's in your interest to make sure that you can also cancel the shut down.
â Shadow
23 mins ago
This is a very important point. Whatever you implement - it's in your interest to make sure that you can also cancel the shut down.
â Shadow
23 mins ago
add a comment |Â
up vote
3
down vote
First of all, welcome.
Although present answers here all satisfy the requirement to the perfection, I have a humble unsolicited suggestion, that's if you'd like your machine to power off as soon as the tasks are done.
bash
scripts can be trap
ped, meaning certain signals can be intercepted and certain tasks can be executed when needed. EXIT
is one of the signals that can be trapped.
You would be able to:
- Set a
trap
forEXIT
of your automated shell scripts, meaning
termination of your automated tasks - Set a
trap
for your.bashrc
EXIT
, meaning whenever you log out
of that machine, power it off.
Option #1 would be the ideal case, provided your tasks don't require adhoc inspection and manual judgement.
Option #2 would cover the cases where you'd forget to exit the terminal without powering off. There is a caveat though, if you have multiple terminals to same machine open and you exit from one of them, it will still power off the machine all the same. (It can be scripted to avoid that, but I won't complicate the solution.)
cleanup()
# Do some tasks before terminating
echo oh la lah, cleaning is so nice
echo "Cy'a later world"
sudo poweroff & # finally shutdown
trap cleanup EXIT
This can be at the end of .bashrc
for Option #2, at somewhere top of your script for Option #1.
Why not use poweroff
at the end of the script?
I prefer to use set -eo pipefail
in on top of my scripts, if any error happens, it won't silently fail, it will stop executing more commands. trap
of EXIT
signal should cover the cases where script terminates prematurely due to errors.
However for your tasks, this might also mean machine will shutdown before they are completed.
I have a simple bash
template I use for making scripting easier to debug, maybe might be of some use. Please see this gist.
add a comment |Â
up vote
3
down vote
First of all, welcome.
Although present answers here all satisfy the requirement to the perfection, I have a humble unsolicited suggestion, that's if you'd like your machine to power off as soon as the tasks are done.
bash
scripts can be trap
ped, meaning certain signals can be intercepted and certain tasks can be executed when needed. EXIT
is one of the signals that can be trapped.
You would be able to:
- Set a
trap
forEXIT
of your automated shell scripts, meaning
termination of your automated tasks - Set a
trap
for your.bashrc
EXIT
, meaning whenever you log out
of that machine, power it off.
Option #1 would be the ideal case, provided your tasks don't require adhoc inspection and manual judgement.
Option #2 would cover the cases where you'd forget to exit the terminal without powering off. There is a caveat though, if you have multiple terminals to same machine open and you exit from one of them, it will still power off the machine all the same. (It can be scripted to avoid that, but I won't complicate the solution.)
cleanup()
# Do some tasks before terminating
echo oh la lah, cleaning is so nice
echo "Cy'a later world"
sudo poweroff & # finally shutdown
trap cleanup EXIT
This can be at the end of .bashrc
for Option #2, at somewhere top of your script for Option #1.
Why not use poweroff
at the end of the script?
I prefer to use set -eo pipefail
in on top of my scripts, if any error happens, it won't silently fail, it will stop executing more commands. trap
of EXIT
signal should cover the cases where script terminates prematurely due to errors.
However for your tasks, this might also mean machine will shutdown before they are completed.
I have a simple bash
template I use for making scripting easier to debug, maybe might be of some use. Please see this gist.
add a comment |Â
up vote
3
down vote
up vote
3
down vote
First of all, welcome.
Although present answers here all satisfy the requirement to the perfection, I have a humble unsolicited suggestion, that's if you'd like your machine to power off as soon as the tasks are done.
bash
scripts can be trap
ped, meaning certain signals can be intercepted and certain tasks can be executed when needed. EXIT
is one of the signals that can be trapped.
You would be able to:
- Set a
trap
forEXIT
of your automated shell scripts, meaning
termination of your automated tasks - Set a
trap
for your.bashrc
EXIT
, meaning whenever you log out
of that machine, power it off.
Option #1 would be the ideal case, provided your tasks don't require adhoc inspection and manual judgement.
Option #2 would cover the cases where you'd forget to exit the terminal without powering off. There is a caveat though, if you have multiple terminals to same machine open and you exit from one of them, it will still power off the machine all the same. (It can be scripted to avoid that, but I won't complicate the solution.)
cleanup()
# Do some tasks before terminating
echo oh la lah, cleaning is so nice
echo "Cy'a later world"
sudo poweroff & # finally shutdown
trap cleanup EXIT
This can be at the end of .bashrc
for Option #2, at somewhere top of your script for Option #1.
Why not use poweroff
at the end of the script?
I prefer to use set -eo pipefail
in on top of my scripts, if any error happens, it won't silently fail, it will stop executing more commands. trap
of EXIT
signal should cover the cases where script terminates prematurely due to errors.
However for your tasks, this might also mean machine will shutdown before they are completed.
I have a simple bash
template I use for making scripting easier to debug, maybe might be of some use. Please see this gist.
First of all, welcome.
Although present answers here all satisfy the requirement to the perfection, I have a humble unsolicited suggestion, that's if you'd like your machine to power off as soon as the tasks are done.
bash
scripts can be trap
ped, meaning certain signals can be intercepted and certain tasks can be executed when needed. EXIT
is one of the signals that can be trapped.
You would be able to:
- Set a
trap
forEXIT
of your automated shell scripts, meaning
termination of your automated tasks - Set a
trap
for your.bashrc
EXIT
, meaning whenever you log out
of that machine, power it off.
Option #1 would be the ideal case, provided your tasks don't require adhoc inspection and manual judgement.
Option #2 would cover the cases where you'd forget to exit the terminal without powering off. There is a caveat though, if you have multiple terminals to same machine open and you exit from one of them, it will still power off the machine all the same. (It can be scripted to avoid that, but I won't complicate the solution.)
cleanup()
# Do some tasks before terminating
echo oh la lah, cleaning is so nice
echo "Cy'a later world"
sudo poweroff & # finally shutdown
trap cleanup EXIT
This can be at the end of .bashrc
for Option #2, at somewhere top of your script for Option #1.
Why not use poweroff
at the end of the script?
I prefer to use set -eo pipefail
in on top of my scripts, if any error happens, it won't silently fail, it will stop executing more commands. trap
of EXIT
signal should cover the cases where script terminates prematurely due to errors.
However for your tasks, this might also mean machine will shutdown before they are completed.
I have a simple bash
template I use for making scripting easier to debug, maybe might be of some use. Please see this gist.
answered 10 hours ago
sdkks
1836
1836
add a comment |Â
add a comment |Â
up vote
2
down vote
Try to put the script (sudo shutdown -P 3600) in the /etc/init.d directory to run it automatically at startup.
Or try using anacron, adding the command in the /etc/anacrontab file.
I also suggest to use nohup in front of the command to be sure that the command still working after logout
New contributor
Depending on distro, it might not get executed by just being in init.d
â sdkks
11 hours ago
@sdkks, you're right. Anacron can be also considered. putting the script in /etc/anacrontab, also used with 'nohup' in the event of logout.
â Saveriofr
11 hours ago
@Saveriofr You can edit your answer to improve its quality (better than posting additional info as a comment).
â Anthony Geoghegan
11 hours ago
add a comment |Â
up vote
2
down vote
Try to put the script (sudo shutdown -P 3600) in the /etc/init.d directory to run it automatically at startup.
Or try using anacron, adding the command in the /etc/anacrontab file.
I also suggest to use nohup in front of the command to be sure that the command still working after logout
New contributor
Depending on distro, it might not get executed by just being in init.d
â sdkks
11 hours ago
@sdkks, you're right. Anacron can be also considered. putting the script in /etc/anacrontab, also used with 'nohup' in the event of logout.
â Saveriofr
11 hours ago
@Saveriofr You can edit your answer to improve its quality (better than posting additional info as a comment).
â Anthony Geoghegan
11 hours ago
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Try to put the script (sudo shutdown -P 3600) in the /etc/init.d directory to run it automatically at startup.
Or try using anacron, adding the command in the /etc/anacrontab file.
I also suggest to use nohup in front of the command to be sure that the command still working after logout
New contributor
Try to put the script (sudo shutdown -P 3600) in the /etc/init.d directory to run it automatically at startup.
Or try using anacron, adding the command in the /etc/anacrontab file.
I also suggest to use nohup in front of the command to be sure that the command still working after logout
New contributor
edited 9 hours ago
New contributor
answered 12 hours ago
Saveriofr
173
173
New contributor
New contributor
Depending on distro, it might not get executed by just being in init.d
â sdkks
11 hours ago
@sdkks, you're right. Anacron can be also considered. putting the script in /etc/anacrontab, also used with 'nohup' in the event of logout.
â Saveriofr
11 hours ago
@Saveriofr You can edit your answer to improve its quality (better than posting additional info as a comment).
â Anthony Geoghegan
11 hours ago
add a comment |Â
Depending on distro, it might not get executed by just being in init.d
â sdkks
11 hours ago
@sdkks, you're right. Anacron can be also considered. putting the script in /etc/anacrontab, also used with 'nohup' in the event of logout.
â Saveriofr
11 hours ago
@Saveriofr You can edit your answer to improve its quality (better than posting additional info as a comment).
â Anthony Geoghegan
11 hours ago
Depending on distro, it might not get executed by just being in init.d
â sdkks
11 hours ago
Depending on distro, it might not get executed by just being in init.d
â sdkks
11 hours ago
@sdkks, you're right. Anacron can be also considered. putting the script in /etc/anacrontab, also used with 'nohup' in the event of logout.
â Saveriofr
11 hours ago
@sdkks, you're right. Anacron can be also considered. putting the script in /etc/anacrontab, also used with 'nohup' in the event of logout.
â Saveriofr
11 hours ago
@Saveriofr You can edit your answer to improve its quality (better than posting additional info as a comment).
â Anthony Geoghegan
11 hours ago
@Saveriofr You can edit your answer to improve its quality (better than posting additional info as a comment).
â Anthony Geoghegan
11 hours ago
add a comment |Â
up vote
1
down vote
If you're really concerned about security, use an mechanical outlet timer on the power source. Just set it to whatever time you want it to shutdown. This way no one can login remotely and disable the shutdown. You would need physical access.
add a comment |Â
up vote
1
down vote
If you're really concerned about security, use an mechanical outlet timer on the power source. Just set it to whatever time you want it to shutdown. This way no one can login remotely and disable the shutdown. You would need physical access.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
If you're really concerned about security, use an mechanical outlet timer on the power source. Just set it to whatever time you want it to shutdown. This way no one can login remotely and disable the shutdown. You would need physical access.
If you're really concerned about security, use an mechanical outlet timer on the power source. Just set it to whatever time you want it to shutdown. This way no one can login remotely and disable the shutdown. You would need physical access.
answered 6 hours ago
gogators
2671210
2671210
add a comment |Â
add a comment |Â
jmhostalet is a new contributor. Be nice, and check out our Code of Conduct.
jmhostalet is a new contributor. Be nice, and check out our Code of Conduct.
jmhostalet is a new contributor. Be nice, and check out our Code of Conduct.
jmhostalet is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f472718%2fhow-to-shut-down-linux-server-after-running-for-60-minutes%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
2
Have a look at
at
(one-time execution).â dirkt
13 hours ago
3
I haven't done this, but your login script could start a
sudo shutdown -h +60
which would start a countdown counter of 60 mins for the shutdown (-halt) process. If you wanted to cancel it, you couldsudo shutdown -c
(this doesn't use cron though)â guiverc
13 hours ago
I might have a solution that will shut it down as soon as your task is done. May I know how you run these tasks, by a bash script?
â sdkks
11 hours ago
5
Depending on the nature of your tasks maybe it's worth running them inside a docker container, so you don't need to worry about switching off a server. The container is created, it runs your tasks, and it's destroyed after that.
â Vicente Olivert Riera
11 hours ago