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 |
You could poll for the PID with ps and then call shutdown when it is no longer found.
|
Quote:
|
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 |
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... |
You can use pgrep to find your process ID.
Code:
pgrep -lf name_of_executable Code:
while ps -p 99999 > /dev/null; do |
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..." |
Quote:
|
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. |
Quote:
|
Quote:
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 & |
I made the following changes:
Added this line to /etc/sudoers Code:
username ALL=NOPASSWD:/bin/systemctl poweroff Code:
$ while ps -p 1311 > /dev/null ; do echo -ne 'Process running...\r' ; sleep 10m ; done ; sudo systemctl poweroff |
Quote:
Code:
$ dd if=/dev/zero of=/dev/null |
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. |
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. |