0

I am trying to put a python script on a VPS running CentOS 7, that crawls some stock data online from a web service. What I want to do is to set this script as an OS service that start/stops at a specific time.

How can I do it?

EDIT:this is the result of systemctl status python-script

   Loaded: loaded (/etc/systemd/system/python-script.service; enabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Sat 2020-05-16 01:11:50 +0430; 15h ago
 Main PID: 854 (code=exited, status=1/FAILURE)

May 16 01:11:43 boiga.server1.more.com systemd[1]: Started Python Script Serv...
May 16 01:11:50 boiga.server1.more.com python3[854]: Traceback (most recent c...
May 16 01:11:50 boiga.server1.more.com python3[854]: File "/root/script.py", ...
May 16 01:11:50 boiga.server1.more.com python3[854]: await (websocketConnect())
May 16 01:11:50 boiga.server1.more.com python3[854]: NameError: name 'await' ...
May 16 01:11:50 boiga.server1.more.com systemd[1]: python-script.service: mai...
May 16 01:11:50 boiga.server1.more.com systemd[1]: Unit python-script.service...
May 16 01:11:50 boiga.server1.more.com systemd[1]: python-script.service failed.
Hint: Some lines were ellipsized, use -l to show in full.

1 Answer 1

2

You can create a service that runs this python script by creating a service file /etc/systemd/system/python-script.service , like this example :

[Unit]
Description=Python Script Service
After=network.target

[Service]
Type=simple
User=root
ExecStart=/usr/bin/python3 /root/script.py
Restart=on-abort


[Install]
WantedBy=multi-user.target

then run systemctl daemon-reload to reload systemd and systemctl enable python-script to enable the service.

Running the service should give you something like this:

systemctl status python-script
● python-script.service - Python Script Service
   Loaded: loaded (/etc/systemd/system/python-script.service; enabled; vendor preset: disabled)
   Active: inactive (dead) since Wed 2020-05-13 23:10:30 CEST; 2s ago
  Process: 27405 ExecStart=/usr/bin/python3 /root/script.py (code=exited, status=0/SUCCESS)
 Main PID: 27405 (code=exited, status=0/SUCCESS)

May 13 23:10:30 server1 systemd[1]: Started Python Script Service.
May 13 23:10:30 server1 python3[27405]: Hello World!

Regarding planning the start/stop of the service, you can use crontab for that

6
  • Hi anisk, thank you for help. Can you more explain about crontab and how to use it? Commented May 14, 2020 at 9:04
  • @user3486308, You can use crontab -e to edit crontabs and add two crons if you want, one that starts the service and another that stops it, a simple example is the following : 00 8 * * * systemctl start python-script 2&1 >/dev/null 15 18 * * * systemctl stop python-script 2&1 >/dev/null Commented May 14, 2020 at 9:16
  • When I run systemctl enable python-script it shows nothing! Commented May 15, 2020 at 20:22
  • what it the name of the file that you've created in /etc/systemd/system/ and what is the output of systemctl status python-script ? Commented May 16, 2020 at 8:34
  • 1
    Obviously now you have a service recognized by systemd. The log is truncated, but shows an error in you application on python level ( NameError: name 'await' ). Have you tested separately the command /usr/bin/python3 /root/script.py? Commented May 17, 2020 at 9:08

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.