7

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.

3 Answers 3

3

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
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your answer, Urban48. But it seemed to me very difficult. can you show an example of a simple python script that you need to run in a virtual environment? Or maybe you know a book where it is described for beginners?
@asergey, its not hard at all. i outlined exactly what you need to do in my answer, just few easy steps. take the time to read a little about it, or watch a video.
2

You can use the screen command, it works perfectly.

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

2 Comments

Is it like tmux?
I think it is better to redirect you to the following stack exchange discussion superuser.com/questions/236158/tmux-vs-screen Please mark your question as answered if you have found what you looked for
-1

You can use a simple command, from the env directory:

(env)$ python /path/to/createdb.py > logger.txt 2>&1 &

This will help for storing the program logs in a defined file called "logger.txt"

1 Comment

Without screen or tmux or nohup, your background process will be killed whenever the terminal or shell is closed or you exit the ssh session.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.