Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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:
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.
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...
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.
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.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.