LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-28-2020, 07:10 AM   #1
ddenial
Member
 
Registered: Dec 2016
Distribution: CentOS, Fedora, Ubuntu
Posts: 359

Rep: Reputation: 56
Shutdown PC after command is done?


Hello All

Is there a way to shutdown PC after the existing command is done executing?

I have this shell script which takes one or more hours to finish. Sometimes in the middle of the runtime, I have to leave PC and go out for other work. I have a dilemma - should I cancel the running process by Ctrl+C and shutdown PC or just leave it running. Most of the time I return after several hours, in this time the PC would be sitting there idle consuming power after it has finished the command.

So my question - Is there a way to append like '&& systemctl shutdown' to existing command/process which is already executing?

Thanks
 
Old 05-28-2020, 07:13 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,337
Blog Entries: 3

Rep: Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732
You could poll for the PID with ps and then call shutdown when it is no longer found.
 
2 members found this post helpful.
Old 05-28-2020, 07:18 AM   #3
ddenial
Member
 
Registered: Dec 2016
Distribution: CentOS, Fedora, Ubuntu
Posts: 359

Original Poster
Rep: Reputation: 56
Quote:
Originally Posted by Turbocapitalist View Post
You could poll for the PID with ps and then call shutdown when it is no longer found.
Poll? Would you guide me on how this works?
 
Old 05-28-2020, 07:21 AM   #4
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS, Manjaro
Posts: 5,718

Rep: Reputation: 2735Reputation: 2735Reputation: 2735Reputation: 2735Reputation: 2735Reputation: 2735Reputation: 2735Reputation: 2735Reputation: 2735Reputation: 2735Reputation: 2735
Or you can explicitly do a shutdown. If you know you are entring the situation start your script (in example I will call it runstuff) with a line like this:
Code:
runstuff;wait:sudo shutdown -h
and see if that works the way you want.
 
1 members found this post helpful.
Old 05-28-2020, 07:34 AM   #5
dc.901
Senior Member
 
Registered: Aug 2018
Location: Atlanta, GA - USA
Distribution: CentOS/RHEL, openSuSE/SLES, Ubuntu
Posts: 1,005

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
Let me ask and give you some hints:
1) How do you check to see if your process is running?
2) How can you keep repeating that check until it is not found? Hint: while loop
3) Add a wait - within while loop (hint: sleep command) unless you want to check constantly.

There are many other ways to accomplish #2, and while loop is just one of the many ways.


If you get stuck, feel free to ask...
 
Old 05-28-2020, 07:54 AM   #6
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,337
Blog Entries: 3

Rep: Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732
You can use pgrep to find your process ID.

Code:
pgrep -lf name_of_executable
Then for polling, substitute the right PID for 99999 below.

Code:
while ps -p 99999 > /dev/null; do
        sleep 60;
done;
shutdown -h +2 'process 99999 is done';
It's a little clumsy and there are probably better methods, especially if you plan from the start.
 
2 members found this post helpful.
Old 05-28-2020, 08:33 AM   #7
ddenial
Member
 
Registered: Dec 2016
Distribution: CentOS, Fedora, Ubuntu
Posts: 359

Original Poster
Rep: Reputation: 56
I executed this in another terminal tab.
Code:
$ while ps -p 1313 > /dev/null ; do echo -ne 'Process running...\r' ; sleep 10m ; done ; shutdown -h +5 "Bye Bye..."
Is this the way? I'm getting 'Must be root' error.
 
Old 05-28-2020, 08:49 AM   #8
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,718

Rep: Reputation: 7973Reputation: 7973Reputation: 7973Reputation: 7973Reputation: 7973Reputation: 7973Reputation: 7973Reputation: 7973Reputation: 7973Reputation: 7973Reputation: 7973
Quote:
Originally Posted by ddenial View Post
I executed this in another terminal tab.
Code:
$ while ps -p 1313 > /dev/null ; do echo -ne 'Process running...\r' ; sleep 10m ; done ; shutdown -h +5 "Bye Bye..."
Is this the way? I'm getting 'Must be root' error.
That's ONE way...and think about what you're doing; you're trying to shut down the system, so you NEED root/sudo to do that. And if you have a shell script already, not even sure why you'd mess about with PID's and things. As suggested by wpeckham in post#4, can't you just put "shutdown -h 5" at the END of your script?? Run it as sudo/root, or put it in cront....system will run script, and the last part is a shutdown. Simple. Doesn't need a PID or anything else...
 
Old 05-28-2020, 08:55 AM   #9
dmchess
Member
 
Registered: Jan 2005
Posts: 244

Rep: Reputation: 43
I am curious, what kind of script are you running that takes several hours to run?
Maybe it's time to rework it into a compiled program like c or pascal.
 
1 members found this post helpful.
Old 05-28-2020, 09:05 AM   #10
ddenial
Member
 
Registered: Dec 2016
Distribution: CentOS, Fedora, Ubuntu
Posts: 359

Original Poster
Rep: Reputation: 56
Quote:
Originally Posted by dmchess View Post
I am curious, what kind of script are you running that takes several hours to run?
Maybe it's time to rework it into a compiled program like c or pascal.
Encoding long videos to HEVC 10Bit. https://www.linuxquestions.org/quest...rd-4175675084/
 
Old 05-28-2020, 09:34 AM   #11
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,337
Blog Entries: 3

Rep: Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732
Quote:
Originally Posted by ddenial View Post
I executed this in another terminal tab.
Code:
$ while ps -p 1313 > /dev/null ; do echo -ne 'Process running...\r' ; sleep 10m ; done ; shutdown -h +5 "Bye Bye..."
Is this the way? I'm getting 'Must be root' error.
You can run the loop as root or you can run just the shutdown as root using sudo. If you run the shutdown as root, then you need to set shutdown, and only shutdown, to run under sudo without a password for just your account. I'd recommend the first method.

If you are launching the task again and you are using Bash for your shell, then the wait built-in command can wait for your task to finish.

Code:
your_task_script &
wait 99999; sudo shutdown -h +5 'bye bye';
Again, use the PID of the process which has been sent to the background. However, all that has to be in the same shell session as the task itself.
 
1 members found this post helpful.
Old 05-28-2020, 09:49 AM   #12
ddenial
Member
 
Registered: Dec 2016
Distribution: CentOS, Fedora, Ubuntu
Posts: 359

Original Poster
Rep: Reputation: 56
I made the following changes:

Added this line to /etc/sudoers
Code:
username ALL=NOPASSWD:/bin/systemctl poweroff
The command:
Code:
$ while ps -p 1311 > /dev/null ; do echo -ne 'Process running...\r' ; sleep 10m ; done ; sudo systemctl poweroff
Working now. So far so good.
 
Old 05-28-2020, 10:03 AM   #13
ddenial
Member
 
Registered: Dec 2016
Distribution: CentOS, Fedora, Ubuntu
Posts: 359

Original Poster
Rep: Reputation: 56
Quote:
Originally Posted by Turbocapitalist View Post
You can run the loop as root or you can run just the shutdown as root using sudo. If you run the shutdown as root, then you need to set shutdown, and only shutdown, to run under sudo without a password for just your account. I'd recommend the first method.

If you are launching the task again and you are using Bash for your shell, then the wait built-in command can wait for your task to finish.

Code:
your_task_script &
wait 99999; sudo shutdown -h +5 'bye bye';
Again, use the PID of the process which has been sent to the background. However, all that has to be in the same shell session as the task itself.
Hmmm. I tested by running the dd command and killed it from another terminal. Working fine.

Code:
$ dd if=/dev/zero of=/dev/null
^Z
[1]+  Stopped                 dd if=/dev/zero of=/dev/null

$ bg
[1]+ dd if=/dev/zero of=/dev/null &

$ jobs -l
[1]+  1350 Running                 dd if=/dev/zero of=/dev/null &

$ wait 1350 ; sudo systemctl poweroff

[1]+  Terminated              dd if=/dev/zero of=/dev/null
Connection to rh7 closed by remote host.
Connection to rh7 closed.
Thanks
 
Old 05-28-2020, 10:11 AM   #14
ddenial
Member
 
Registered: Dec 2016
Distribution: CentOS, Fedora, Ubuntu
Posts: 359

Original Poster
Rep: Reputation: 56
So, just to clarify, when I run the script in the foreground, it takes most of the resources. Will this be the same when running in the background? It won't run in low priority, right?

Nevermind. Checked top, the background process takes same amount of resources.

Last edited by ddenial; 05-28-2020 at 10:47 AM.
 
Old 05-31-2020, 03:34 PM   #15
Hermani
Member
 
Registered: Apr 2018
Location: Delden, NL
Distribution: Ubuntu
Posts: 261
Blog Entries: 3

Rep: Reputation: 113Reputation: 113
Maybe I am just a fan of simple solutions. But I am still a n--b as well..

What is wrong with something like

Code:
> command_to_run && shutdown now
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Spectre and Meltdown: Explanation, Info, What's being done, What can be done Zyblin Linux - Security 7 02-17-2018 09:48 PM
what is the default powerstate (e.g. S3,S4,S5) for the command 'shutdown -hP' or 'shutdown -hH' or 'shutdown -h' badbetty Slackware 6 11-12-2017 12:18 AM
FC18: running a command on shutdown (or reboot) before every other shutdown operation P5music Fedora 3 04-23-2013 03:56 PM
'shutdown' command causing unclean shutdown silentwol Linux - Software 3 07-01-2009 02:01 AM
suse 10.0 => processes "sending kill signal": "done, done, missing"...!? ungua Linux - Newbie 2 01-18-2006 10:52 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:16 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration