Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In bash, how can I initiate a job in a stopped state, as if I started it normally and then immediately pressed Ctrl-Z?

Or as if I had sent SIGSTOP to the process immediately, but without giving the process a chance to execute before it receives the SIGSTOP.

share|improve this question
    
Duplicate of serverfault.com/questions/293632/… ? – Foon Oct 7 '12 at 11:39
    
Yes, it's an exact duplicate. Unfortunately the other question also has not been answered to my satisfaction. – nibot Oct 7 '12 at 11:47

You can start a new subshell, immediatelly stop it, and then (i.e. after makint it run again) run your command. I used the $BASHPID variable to get the PID of the subshell, as $$ still returns the PID of the parent:

( kill -SIGSTOP $BASHPID; exec my_command )

Using exec here will cause the my_command process to use the same PID as the subshell.

share|improve this answer
    
Change it to ( kill -SIGSTOP $BASHPID; exec my_command ) and I think we have a winner! – nibot Oct 7 '12 at 12:10

I know this question is quite old, but just in case someone is looking for this functionality, like I was:

I recently added this to my LD_PRELOAD module collection (https://github.com/zardus/preeny). Using preeny, you can have a process suspend itself on startup:

# https://github.com/zardus/preeny
# cd preeny
# make
# LD_PRELOAD=x86_64-linux-gnu/startstop.so /bin/echo TEST
[1]+  Stopped    LD_PRELOAD=x86_64-linux-gnu/startstop.so /bin/echo TEST
# fg
TEST

Preeny suspends the process during library startup, before main() is called.

share|improve this answer
1  
Hi Zardus, and welcome to Super User. No worries about answering an old question. This is an interesting approach! – Michael Kjörling May 10 '15 at 21:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .