How to run a Python program in the background on an Ubuntu server

I have a python script. Script have selenium with Chrome and go to the site, take the data and put it in a CSV file.
This is a very long job.
I put Script on the server. And run. Everything works. But I need Script to run 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 starts 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.

+5
source share
2 answers

You can use the screen command, it works great.

Here is a very good link: https://www.rackaid.com/blog/linux-screen-tutorial-and-how-to/

+1
source

You can create a background daemon (service)
You participated in Ubuntu 16.04, this means that you got systemd, for more information on how to configure it, visit this link

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

systemd might 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 reboot the system and start the service:

 sudo systemctl daemon-reload sudo systemctl myservice start 
+2
source

Source: https://habr.com/ru/post/1275392/


All Articles