student_1
student_1

Reputation: 43

Linux script with nohup inside it does not terminate

Consider the following Linux script (script.sh)

#!/bin/bash
echo "Script started"
nohup python3 run_process.py > /dev/null 2>&1 &
echo "Script finished"

The script runs fine and starts in background the process "run_process.py", as expected, but the script itself keeps showing up as running (with the command ps x for example). I have tried disown, setsid and other solutions found in other questions, but none of them work.

So what is the correct way to write a .sh script that contains a line with nohup, such that the script correctly terminates when finished ?

Edit 1: This is a complete prototype of the script:

#!/bin/bash
echo "Script started"
pkill -f process_1.py
sleep 1
cd /to_some_file && python3 program_1.py
sleep 1
cd /to_some_file/ && nohup python3 run_process_1.py > /dev/null 2>&1 &
sleep 1
pkill -f process_2.py
sleep 1
cd /to_some_file && python3 program_2.py
sleep 1
cd /to_some_file/ && nohup python3 run_process_2.py > /dev/null 2>&1 &
echo "Script finished"
exit 0

To add some context, the script is used to:

  1. stop some processes running in background,
  2. perform updates by running some programs,
  3. and then restart the processes.

Upvotes: 3

Views: 93

Answers (0)

Related Questions