1

On previous questions similar to mine, the proposed solutions are all involved suspending the current foreground task with Ctrl+Z and then running fg && ... or fg ; ... (and maybe sending the old task to the background bg first, so that it can continue running while the new command is being typed).

At least for me this only works with a single queued command. When I try using this trick multiple times in a row, the middle commands are forgotten:

$ sleep 60 && echo test1

^Z
[1]  + 2336348 suspended  sleep 60

$ fg && echo test2
[1]  + 2336348 continued  sleep 60

^Z
[1]  + 2336348 suspended  sleep 60

$ fg && echo test3
[1]  + 2336348 continued  sleep 60

test3

Note how test1 was never printed (though I could live with that, this is just because my initial command consisted of two and my sleep 60 was fully executed, which is what I care about most). More importantly, note how test2 was never printed. This was my second command and I believe it was never executed.

What I want is to enqueue another command that I just thought of (while other commands were running) so that it runs after the current command and after/before all other commands that I have enqueued before. The ordering of the commands ultimately doesn't matter, but they cannot run simultaneously (otherwise I could just open another shell and run them there) because they depend on some common state (file system or so).

Thanks for your help!

0

If you're on a GNU/Linux system or have coreutils installed, use flock(1) (maybe using the executable/command itself as the lockfile):

% cat linger
echo -n $$; date +%s; sleep $1

% flock ./linger ./linger 8 &
[3] 5941
% 59421598557788

% flock ./linger ./linger 9 &
[4] 5945
% flock ./linger ./linger 11 &
[5] 5946
% 59471598557796
59501598557805

[3]   Done                    flock ./linger ./linger 8
[4]   Done                    flock ./linger ./linger 9
[5]   Done                    flock ./linger ./linger 11

What you're trying to do doesn't work, because for the shell stopping a command is like it immediately exiting with a non-zero (fail) status:

% sleep 10 || echo FAIL
^Z
[3]+  Stopped                 sleep 10
FAIL
% fg
sleep 10
% echo $?
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.