1

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.

Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
Manvi
  • 1,136
  • 2
  • 18
  • 41
  • Is there anything in `nohup.out`? – Barmar Oct 09 '18 at 20:57
  • No nohup.out is empty – Manvi Oct 09 '18 at 20:57
  • Also there might be something in `nohup.err` – Zak Oct 09 '18 at 20:57
  • I do not see any file with name nohup.err is created – Manvi Oct 09 '18 at 20:58
  • 2
    You said "After doing ctrl-C, I see the logs are coming every minute." Do you need to exit the program for these logs to start coming?! – hesham_EE Oct 09 '18 at 21:01
  • yes python script is running apschedular – Manvi Oct 09 '18 at 21:01
  • If you don't flush your writes, they get buffered when not to a TTY by default. If you wait for several kb to accumulate, they'll eventually show up. – Charles Duffy Oct 09 '18 at 22:57
  • BTW, this is not in any way related to `nohup`. You'd have 100% identical behavior with `test.sh >my.out 2>&1` with no `nohup` involved. (For that matter, if your deployment environment is guaranteed to run bash, using the nonstandard `nohup` command is completely pointless: The shell's built-in `disown` command can do everything `nohup` can and more). – Charles Duffy Oct 09 '18 at 22:58
  • Note too that `nohup` only redirects stdout and stderr -- it does not redirect content which would be written to a separate log file; and for that matter, it doesn't redirect stdout or stderr *if they were already open to a non-TTY destination when it was invoked*. – Charles Duffy Oct 09 '18 at 23:01

1 Answers1

2

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

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
m21
  • 66
  • 1
  • 7