Reputation: 1479
I want to use shell script to launch Redis server and then monitor a log file:
#!/bin/bash
/path/to/redis/src/redis-server &
tail -f /path/to/log/logfile.log
If I run this script and press Ctrl+C from the terminal, the tail -f
terminated, which is what I want, however the Redis also detected SIGINT and exited.
I tried to write the script like this:
#!/bin/bash
trap '' INT TSTP
~/redis/src/redis-server &
tail -f ./script1
This time things go even worse, the tail -f
refused to terminate while Redis still detected SIGINT and exited.
It seems that there is some problems specific to Redis regarding ignoring signals.
My goal is to make tail -f
responds to Ctrl+C while making Redis ignore this signal.
Please anyone tell me whether this can be achieved and if so, give me some advice?
Upvotes: 0
Views: 569
Reputation: 10271
redis-server
catches SIGINT
(Ctrl+C), even if SIGINT
was being ignored. This is an unusual choice; most software will check and won't catch SIGINT
if it's already being ignored.
When it receives SIGINT
, it saves the database and shuts down.
If you start it as a service, it won't be associated with any terminal at all, and won't see any Ctrl+C you type.
If you start it as a background job in an interactive shell:
$ /path/to/redis/src/redis-server &
your shell will put it into a process group that is different from the terminal's process group, and typing Ctrl+C won't affect it. (If you bring it to the foreground with fg
, Ctrl+C will send SIGINT
to the program).
But, when you run a script like this:
#!/bin/bash
/path/to/redis/src/redis-server &
tail -f /path/to/log/logfile.log
the shell that runs the script will be non-interactive, and any program that it starts in the background (with &
) will be in the same process group as the shell. So if you run that shell script in the foreground, typing Ctrl+C will send SIGINT
to the shell, to redis-server
, and to tail
.
To prevent Ctrl+C from sending SIGINT
to redis-server
in a case like this, you need to either put redis-server
in its own process group or disassociate it from your terminal. You can do this with setsid
, which does both:
#!/bin/bash
setsid /path/to/redis/src/redis-server &
tail -f /path/to/log/logfile.log
Upvotes: 1