Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run the Python program in the background in Ubuntu server

I have a python script. Script have selenium with Chrome and go to a website, take data and put in CSV file.
This is a very long work.
I put the script on the server. And run. All work. But I need script work in the background.

chmod +x createdb.py
nohup python ./createdb.py &

And I see

(env)$ nohup ./createdb.py &
[1] 32257
(env)$ nohup: ignoring input and appending output to 'nohup.out'

Press Enter.

(env)$ nohup ./createdb.py &
[1] 32257
(env)$ nohup: ignoring input and appending output to 'nohup.out'
[1]+  Exit 1                  nohup ./createdb.py

Then it runs and immediately writes errors to the file, that Chrome did not start or there was no click.
I want to remind you that if you start without nohup, then everything will work.
What am I doing wrong? How to run a script?
Thank you very much.

like image 287
Serhii Avatar asked Feb 16 '18 08:02

Serhii


1 Answers

You could create a background daemon (service)
You taged Ubuntu 16.04 it means you got systemd, for more information on how to set it up, please visit this link

create a file called <my_service>.system and put it there: /etc/systemd/system

you systemd unit could look like this:

[Unit]
Description=my service
After=graphical.target

[Service]
Type=simple
WorkingDirectory=/my_dir
ExecStart=python my_script.py

[Install]
WantedBy=multi-user.target

then all you have to do is, reload systemd manage and start your service:

sudo systemctl daemon-reload
sudo systemctl myservice start
like image 163
Urban48 Avatar answered Sep 18 '22 07:09

Urban48