LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Shutdown PC after command is done? (https://www.linuxquestions.org/questions/linux-newbie-8/shutdown-pc-after-command-is-done-4175676063/)

ddenial 05-28-2020 07:10 AM

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

Turbocapitalist 05-28-2020 07:13 AM

You could poll for the PID with ps and then call shutdown when it is no longer found.

ddenial 05-28-2020 07:18 AM

Quote:

Originally Posted by Turbocapitalist (Post 6128271)
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?

wpeckham 05-28-2020 07:21 AM

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.

dc.901 05-28-2020 07:34 AM

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...

Turbocapitalist 05-28-2020 07:54 AM

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.

ddenial 05-28-2020 08:33 AM

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.

TB0ne 05-28-2020 08:49 AM

Quote:

Originally Posted by ddenial (Post 6128303)
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...

dmchess 05-28-2020 08:55 AM

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.

ddenial 05-28-2020 09:05 AM

Quote:

Originally Posted by dmchess (Post 6128311)
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/

Turbocapitalist 05-28-2020 09:34 AM

Quote:

Originally Posted by ddenial (Post 6128303)
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.

ddenial 05-28-2020 09:49 AM

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.

ddenial 05-28-2020 10:03 AM

Quote:

Originally Posted by Turbocapitalist (Post 6128318)
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

ddenial 05-28-2020 10:11 AM

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.

Hermani 05-31-2020 03:34 PM

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


All times are GMT -5. The time now is 09:50 PM.