15

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.

8
  • You can use e.g trap 'kill -s SIGSTOP 1234' EXIT in a bash script. Also it is #!/bin/bash Apr 29, 2015 at 9:33
  • @val0x00ff, I tried trap 'kill -s SIGSTOP 1234' EXIT but I get same error
    – utarid
    Apr 29, 2015 at 9:41
  • 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. Apr 29, 2015 at 10:04
  • 1
    @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.
    – utarid
    Apr 29, 2015 at 10:38
  • 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! Apr 29, 2015 at 10:45

3 Answers 3

16

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.

4
  • 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. Apr 29, 2015 at 11:18
  • 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. Apr 29, 2015 at 11:23
  • @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. Apr 29, 2015 at 12:09
  • Well ok, I didn't notice the -SIGSTOP I thought passing -s to kill was mandatory. Thanks for the explaination! Learnt something new today. Apr 29, 2015 at 12:20
3

I am facing the same problem. All answers here did not address the true cause of the issue. Inside the script the kill used is the built-in provided by the shell, that is why you are having the error. While in your terminal, the kill used is the separate executable located at /bin/kill. To fix the issue, inside your script you must explicitly use the /bin/kill and not the built-in shell version. Therefore you write it like this:

#!/bin/sh
/bin/kill -s SIGHUP 14344
2

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
4
  • I think it will be better answer if you add something about #!/bin/bash
    – utarid
    Apr 29, 2015 at 10:40
  • @user4757345 if that solved your problem, just modify the answer and add the explanation. Apr 29, 2015 at 10:46
  • 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. Apr 29, 2015 at 11:07
  • 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? Apr 29, 2015 at 11:09

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.