Zsh Command Line – Prevent Second Command in a Chain

command linejob-controlshutdownzsh

Yesterday before going to sleep I started a long process of which I thought it would be finished before I stand up, therefore I used

./command && sudo poweroff

my system is configured to not ask for a password for sudo poweroff, so it should shutdown when that command is finished.

However it is still running and I want to use that system for other tasks now. Having that command running in the background is not an issue, but having my system possibly shutting down any second is.

Is there a way to prevent zsh from executing the poweroff command while making sure that the first one runs until it is done?

Would editing the /etc/sudoers file so that the system asks for my password still help in this case?

Best Answer

As you clarified in comments it's still running in foreground on an interactive shell, you should just be able to press Ctrl+Z.

That will suspend the ./command job. Unless ./command actually intercepts the SIGTSTP signal and chooses to exit(0) in that case (unlikely), the exit status will be non-0 (128+SIGTSTP, generally 148), so sudo poweroff will not be run.

Then, you can resume ./command in foreground or background with fg or bg.

You can test with:

sleep 10 && echo poweroff

And see that poweroff is not output when you press Ctrl+Z and resume later with fg/bg.

Or with

sleep 10 || echo "failed: $?"

And see failed: 148 as soon as you press Ctrl+Z.

Note that this is valid for zsh and assuming you started it with ./command && sudo poweroff. It may not be valid for other shells, and would not be if you started it some other way such as (./command && sudo poweroff) in a subshell or { ./command && sudo poweroff; } as part of a compound command (which zsh, contrary to most other shells transforms to a subshell so it can be resumed as a whole when suspended).

Related Question