How to determine if a script is currently running using nohup within the same script?

96 Views Asked by At

I have a bash script that needs to be executed with nohup due to its long execution time. How can I verify if the script is currently running with nohup, or if there are alternative methods to ensure this?

I am considering automatically restarting the script with nohup, but I'm concerned if this could cause any issues, especially if it was initially launched with nohup.

I've tried to identify the process using the PID with the commands "ls -l /proc/[PID]/fd" and "ps aux". Unfortunately, these commands do not clearly show whether nohup was used. They merely indicate that the output is being redirected from the terminal to a different location. In addition, while the 'jobs -l' command shows that the script is running with nohup from the terminal, I'm unable to verify this within the script itself because the script runs in its own shell so lacks the necessary access to the jobs of the parent shell

1

There are 1 best solutions below

0
Network Technician On

If GNU screen is installed, you can use the screen command

screen -dmS myprogram script-you-want-to-run.sh

You can then resume by using

screen -r myprogram