0

I have been trying for a few hours to do the following (on Centos 6):

  1. cron starts process A
  2. process A checks if process B is alive
  3. if process B is not alive, process A starts process B

The problem is that after process A terminates, process B is terminated as well.

Process A and Process B are both bash running a script.

I have been trying to use bin/bash script.sh & and exec bin/bash script.sh both without success. If I start process A from the console, process B is not terminated.

Is what I am trying to do theoretically possible (e.g. have a process be started from a process launched from cron and have it survive). In which other ways could I start process B to have it survive the termination of process A?

1

use nohup scriptForB.sh inside scriptForA.sh... in that way, the child process will not be terminated when the parent process exits..

On termination, parent process will send SIGTERM signal to all child process. If you use nohup, the child process will ignore the SIGTERM signals.

4
  • 1
    Actually, it's the SIGHUP that is ignored - hence the name nohup. – Toby Speight Jul 2 '15 at 16:01
  • Thanks for pointing me to nohup. Unfortunately, it still doesn't work. I call from cron nohup run_daemon_from_cron.sh &. In run_daemon_from_cron.sh I call nohup run_daemon.sh &. The process created in run_daemon.sh is still destroyed. If I call run_daemon_from_cron.sh in a serial console, the daemon is started correctly. – mxro Jul 2 '15 at 22:11
  • I added in run_daemon_from_cron.sh a few statements like trap "echo 1 >> /testlog;exit" 1 (for 1,2,3,9,15) but it generates no output. – mxro Jul 2 '15 at 22:30
  • I was having a very similar issue starting a node app in my script. The solution turned out to be: add cd /home/domain/path to where the second script exists. – SingleStepper Sep 8 '19 at 13:47
0

I couldn't solve this problem but this is the workaround which worked for me:

  1. I defined a new script run.sh which does something as the following:

    #!/bin/bash
    
    while true
    do
       run_process_a.sh
       sleep 60
    done
    
  2. Then I defined a couple of other scripts

    • service.sh: A daemon script
    • start.sh: A wrapper to start an instance of run.sh and store its pid
    • stop.sh: A wrapper to stop an instance of run.sh through the stored pid
  3. I registered service.sh with init.d and through chkconfig

As said, this is not a direct answer to my questions but it helped me solve my problem which is why I record it here. Also, this might be the cleaner approach to achieve what I intend.

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.