Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

When I use kill on command line it works.

kill -SIGSTOP 1234

But if I use in bash script file I get this error:

kill: SIGSTOP: invalid signal specification

sh file is

#!/bin/sh
kill -SIGSTOP 1234

How can I use kill in bash script? I tried this:

#!/bin/sh
/bin/bash -c "kill -SIGSTOP 1234"

but it does not work.

share|improve this question
    
You can use e.g trap 'kill -s SIGSTOP 1234' EXIT in a bash script. Also it is #!/bin/bash –  val0x00ff yesterday
    
@val0x00ff, I tried trap 'kill -s SIGSTOP 1234' EXIT but I get same error –  user4757345 yesterday
    
Does the answer I posted illustrate the scenario? Do you still get the error? It should work interactively and as well as within a script. –  val0x00ff yesterday
    
@val0x00ff, my problem solved when I changed #!/bin/sh to #!/bin/bash. I always use #!/bin/sh and now I really do not know why it does not work. –  user4757345 yesterday
    
I see. Well probably because you need to enable posix mode? Are you on a Mac? set +o posix; kill -SIGSTOP 1234 could solve the problem as well! –  val0x00ff yesterday

2 Answers 2

up vote 1 down vote accepted

To start with: SIGSTOP will temporarily stop the process but keep in memory so it can be continued later on using SIGCONT system call. You can use the following little snippet to see what happens

#!/bin/bash

set -x

sleep 100 &
pid=$!
kill -s SIGSTOP "$pid"
sleep 2
kill -s SIGCONT "$pid"

You'll see what the script does interactively.

So to get your script working using #/bin/sh shebang you'd do something like

#!/bin/sh

set -x
set +o posix
sleep 100 &
pid=$!
kill -s SIGSTOP "$pid"
sleep 2
kill -s SIGCONT "$pid"
set -o posix
share|improve this answer
    
I think it will be better answer if you add something about #!/bin/bash –  user4757345 yesterday
    
@user4757345 if that solved your problem, just modify the answer and add the explanation. –  val0x00ff yesterday
1  
👎 POSIX doesn't specify a "posix" option (that's a bash option), and of course does not specify what would happen when you turn it off. Your answer doesn't make any sense. If you want to use a bash specific feature, start the script with #! /bin/bash, don't make it a sh script that assumes sh is bash and then disable the sh mode. –  Stéphane Chazelas yesterday
    
Well according to posix specs, there is no SIG in sh. So again instead of commenting on every answer I post you better edit the answer and provide the right one. How's that? –  val0x00ff yesterday

The standard (POSIX) syntax is:

kill -s STOP "$pid"

That is, without the SIG prefix. Some shell implementations, support kill -s SIGSTOP or kill -SIGSTOP as an extension but that's not standard nor portable.

The Unix specification (POSIX+XSI) also allows:

kill -STOP "$pid"

And

kill -19 "$pid"

Though which signal number is SIGSTOP is not specified and may change between systems and even architectures for a same system, so should be avoided.

share|improve this answer
    
As I explained in my edited answer. set +o posix will allow the usage of SIGTERM, SIGSTOP etc. I do agree that set -o posix at the end is not necessary. It's more a habit. I'm not sure what the significance or the added value is in your answer. –  val0x00ff yesterday
2  
set +o posix will only allow SIGTERM/SIGSTOP in bash when bash is started in posix mode, not with any other implementation of sh. So you rely on your /bin/sh being bash. Don't do that, use #! /bin/bash if you want to use bash specific features. –  Stéphane Chazelas yesterday
    
@val0x00ff Stricktly speaking, pdksh and yash also have a posix option. pdksh supports kill -SIGSTOP regardless of whether that option is enabled or not. yash, like bash also forbids kill -SIGSTOP when the posix option is off, and gives a more specific error message in that case: kill: SIGSTOP: the signal name must be specified without 'SIG'. dash and AT&T ksh kill builtins don't support kill -s SIGSTOP. zsh's and procps-ng's do. –  Stéphane Chazelas yesterday
    
Well ok, I didn't notice the -SIGSTOP I thought passing -s to kill was mandatory. Thanks for the explaination! Learnt something new today. –  val0x00ff 23 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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