nohup not ignoring SIGHUP - python

I am trying to run a python script with chef, and the script will exit when chef exits because of SIGHUP. I am using nohup but it still gets the signal. Any ideas how I can get this script to run in the background?
nohup python simple-setup.py --dbpath /media/ephemeral0/mongo-data/ -n 2 --name dev --arbiters 1 --mongo_path /usr/bin/ > /media/ephemeral0/log/set.log 2>&1 &

I got round a problem like this by putting the command in a shell script, with an & on the end and then nohupping the shell script. If it helps.

Related

Get prompt back after running a python script in background

I am executing a python script in background mode from a shell script.
The script is running fine but I am unable to get the prompt back until I manually press enter from keyboard. I want to avoid the manual interaction.
I already tried running the script like echo "/n" | python script.py &
I also tried:
python3 /simulators/utils/reboot_ap.py $input 2>&1 &`
python3 script.py $input 2>&1 &

nohup with && hangs python

I'm using pysftp in python 3.7 to connect to a Linux box and issue commands.
I am running a dask-worker in the background.
Here's the base command to start a dask-worker:
dask-worker my_domain:8786
Here's the command that gets sent to the Linux box: (you can see I wrap it with nohup in order to allow it to continue to run, and not hang my local python process).
nohup dask-worker my_domain:8786 > /dev/null 2>&1 &
Issuing this command works great! But I have one more thing I want to include, and I can't get it to work with this nohup wrapper. Maybe you can tell me why.
I want to run the dask-worker from a specific folder, like this:
cd /home/user/some_folder/ && dask-worker my_domain:8786
That works great, but when I wrap that in a nohup, it just hangs - my process on my machine issuing commands to Linux boxes doesn't move onto the next command.
So this doesn't work:
nohup cd /home/user/some_folder/ && dask-worker my_domain:8786 > /dev/null 2>&1 &
Do you know why? Or how I can get it to spin off my dask-worker process at that specific location? Is there a better way of doing this than chaining Linux commands together.
Here's the reason I'm doing it this way - because I cannot get the pysftp.chdir() command to actually work. It doesn't change the directory on the Linux box, and maybe that's because it's Centos7, idk, but the prepackaged commands to set a working directory don't stick for me.
Thank you kindly for your attention.

How to run python script at startup

I'm trying to make a Python script run as a service.
It need to work and run automatically after a reboot.
I have tried to copy it inside the init.d folder, But without any luck.
Can anyone help?(if it demands a cronjob, i haven't configured one before, so i would be glad if you could write how to do it)
(Running Centos)
run this command
crontab -e
and then add
#reboot /usr/bin/python /path/to/yourpythonscript
save and quit,then your python script will automatically run after you reboot
There is no intrinsic reason why Python should be different from any other scripting language here.
Here is someone else using python in init.d: blog.scphillips.com/posts/2013/07/… In fact, that deals with a lot that I don't deal with here, so I recommend just following that post.
For Ubuntu Variant:-
open the /etc/rc.local file with:
nano /etc/rc.local
add the following line just before the exit 0 line:
start-stop-daemon -b -S -x python /root/python/test.py
or
Give absolute path of your command i.e
nohup /usr/bin/python2 /home/kamran/auto_run_py_script_1.py &
The start-stop-daemon command creates a daemon to handle the execution of our program. The -b switch causes the program to be executed in the background. The -S switch tells the daemon to start our program. And the -x switch tells the daemon that our program is an executable.
To check and Run
sudo sh /etc/rc.local

Nohup is not writing log to output file

I am using the following command to run a python script in the background:
nohup ./cmd.py > cmd.log &
But it appears that nohup is not writing anything to the log file. cmd.log is created but is always empty. In the python script, I am using sys.stdout.write instead of print to print to standard output. Am I doing anything wrong?
You can run Python with the -u flag to avoid output buffering:
nohup python -u ./cmd.py > cmd.log &
It looks like you need to flush stdout periodically (e.g. sys.stdout.flush()). In my testing Python doesn't automatically do this even with print until the program exits.
Using -u with nohup worked for me. Using -u will force the stdout, stderr streams to be unbuffered. It will not affect stdin. Everything will be saved in "nohup.out " file. Like this-
nohup python -u your_code.py &
You can also save it into your directory. This way-
nohup python -u your_code.py > your_directory/nohup.out &
Also, you can use PYTHONUNBUFFERED. If you set it to a non-empty string it will work same as the -u option. For using this run below commands before running python code.
export PYTHONUNBUFFERED=1
or
export PYTHONUNBUFFERED=TRUE
P.S.- I will suggest using tools like cron-job to run things in the background and scheduled execution.
export PYTHONUNBUFFERED=1
nohup ./cmd.py > cmd.log &
or
nohup python -u ./cmd.py > cmd.log &
https://docs.python.org/2/using/cmdline.html#cmdoption-u
Python 3.3 and above has a flush argument to print and this is the only method that worked for me.
print("number to train = " + str(num_train), flush=True)
print("Using {} evaluation batches".format(num_evals), flush=True)
I had a similar issue, but not connected with a Python process. I was running a script which did a nohup and the script ran periodically via cron.
I was able to resolve the problem by:
redirecting the stdin , stdout and stderr
ensuring the the script being invoked via nohup didn't run anything else in the background
PS: my scripts were written in ksh running on RHEL
I run my scripts in the following way and I have no problem at all:
nohup python my_script.py &> my_script.out &
comparing with your syntax looks like you are only missing a "&" symbol after your input...

Running python script in background with nohup and timing it

I'm running a python script on a remote server with the time command as follows:
time python myscript.py
SSH timeout occurs on the server after some time, so i also need to run it with nohup.So, i have the following two questions:
Is nohup time myscript.py & the right command to execute my python script ?
If the script runs in the background, how will i see the output of the time command ?
Please Help
Thank You
nohup will usually write STDOUT and STDERR to a file called "nohup.out" in the current directory. You'll be able to see the output of time at the end of that file.
Another way of solving this redirection of the output like this:
nohup time bla.py >myoutput &
To 1.: Yes
To 2.: You can redirect the output to a file
nohup time myscript.py > ~/time_output &

Categories