Saturday, February 26, 2022

[SOLVED] Bash script not running in background with nohup

Issue

I have a bash script which activates the anaconda environment and run a python scheduler script and write logs to a file every minute. It is working perfectly fine if I run just the script.

[user@host proj]$ test.sh

After doing ctrl-C, I see the logs are coming every minute.

[user@host proj]$ cat logs/log.log
Test job 1 executed at : 2018-10-09 14:16:00.000787
Test job 1 executed at : 2018-10-09 14:17:00.001890
Test job 1 executed at : 2018-10-09 14:18:00.001861

But when I run same script in background with nohup

[user@host proj]$ nohup test.sh &
[1] 24884
[user@host proj]$ nohup: ignoring input and appending output to ‘nohup.out’

I can see with top that script and python are running

24949 user  20   0  113172   1444   1216 S  0.0  0.0   0:00.00 test.sh
24952 user  20   0  516332  66644  17344 S  0.0  0.8   0:00.65 python

But I cannot see anything to be written in log file.

Not sure what is going wrong. Any suggestion to guide me in correct direction is highly appreciated.


Solution

I'm assuming the proj directory is in your PATH variable.

If nothing is being printed in nohup.out then i think you don't have anything to be echoed to the nohup.out file. Try using set -xv just below your shebang line i.e. #/bin/bash if you are using bash. Now on running the shell script, nohup will have at least one entry of the python script you are running from your shell script as below.

user@host:~/scripts$ cat nohup.out

python /home/madbala/scripts/append_file.py + python /home/madbala/scripts/append_file.py

I have tried to reproduce your situation as below:

  1. Python Script name (sorry but I dont have much idea about python): append_file.py

    #!/usr/bin/python
    from datetime import datetime
    import time
    with open("test.txt", "a") as myfile:
       for x in range(15):
          myfile.write('appended text %s \n' %datetime.now())
          time.sleep(2)
    
  2. Shell Script which calls above python script: run_py_script.sh

      #!/bin/bash
      set -xv
      python /home/madbala/scripts/append_file.py
    

Now on running nohup run_py_script.sh & I get the output as i have set -xv (which actually enables the verbose logging in a shell script - you could also just use set -x).

When i comment out the set -xv using #set -xv nohup.out will have no content.

Screenshot



Answered By - m21
Answer Checked By - Mary Flores (WPSolving Volunteer)