4

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.

nibot
  • 2,147
  • 4
  • 15
  • 18

2 Answers2

6

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.

choroba
  • 20,299
6

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.

Zardus
  • 161