9

I have a python script myScript.py which is writing on a file every 2 second. But when I want to run this script as a systemd service, service works but not writing on file.

I created a myscript.service file on /lib/systemd/system/ and designed as below:

[Unit]
Description=My Script Service
After=multi-user.target

[Service]
Type=idle
ExecStart=/usr/bin/python /home/pala/PycharmProjects/myScript.py

[Install]
WantedBy=multi-user.target

and myScript.py is:

import time
while True:

    with open("/home/pala/Documents/file.txt", "a") as myFile:
        myFile.write("--**--")

    time.sleep(2)
Benyamin Jafari
  • 27,880
  • 26
  • 135
  • 150
pala9323
  • 211
  • 1
  • 3
  • 6
  • Can you post the output of `sudo systemctl status myservice`? I tried this and it worked flawlessly using my home dir. Did you run `sudo systemctl daemon-reload` after adding the service file to `/lib/systemd/system`? Also, what makes you think the service works? – nir0s Mar 11 '17 at 14:12
  • output is: systemctl status myscript.service ● myscript.service - My Script Service Loaded: loaded (/lib/systemd/system/myscript.service; disabled; vendor preset: enabled) Active: active (running) since Cts 2017-03-11 21:54:59 +03; 2min 7s ago Main PID: 16614 (python) CGroup: /system.slice/myscript.service └─16614 /usr/bin/python /home/pala/PycharmProjects/myScript.py Mar 11 21:54:59 palaPC systemd[1]: Started My Script Service. – pala9323 Mar 11 '17 at 18:59
  • Service works there is no any problem as you see above, but my script has to create and write file.txt every 2 seconds. The problem is: file isnt created by the service – pala9323 Mar 11 '17 at 19:03
  • I had written "sudo systemctl daemon-reload" but it hadn't worked. After I reload the computer, services worked and created the file.Thanks for everything. – pala9323 Mar 11 '17 at 19:15
  • Did you at all run `sudo system systemctl start` after creating the service file? – nir0s Mar 12 '17 at 18:28
  • When the service runs, as what user is it running as? Does that user have access to the folder you're trying to write a folder to? – Fran Fitzpatrick Oct 18 '17 at 19:47
  • Have you [checked the output of your service](https://unix.stackexchange.com/questions/20399/view-stdout-stderr-of-systemd-service) using `sudo journalctl -u myscript`? – Nils Werner Jul 09 '18 at 10:23
  • @pala9323 I wrote an answer that works. – Benyamin Jafari Jul 09 '18 at 10:48

2 Answers2

2

This is the procedure of creating a service from your code:

At first, add the following shebang in above of your_script.py:

#!/usr/bin/env python

I use the following instruction for my own services creation:

Suppose your service name is "test", then create files below:

test.service

[Unit]
SourcePath=/etc/init.d/test
[Service]
ExecStart=/etc/init.d/test start
ExecStop=/etc/init.d/test stop

test.sh

#!/usr/bin/env bash

# Quick start-stop-daemon example, derived from Debian /etc/init.d/ssh
set -e

# Must be a valid filename
NAME=this_is_a_test
PIDFILE=/var/run/$NAME.pid
#This is the command to be run, give the full pathname
DAEMON=/home/Your_User_Name/Your_path/your_script.py

case "$1" in
  start)
        echo -n "Starting daemon: "$NAME
    start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- $DAEMON_OPTS
        echo "."
    ;;
  stop)
        echo -n "Stopping daemon: "$NAME
    start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE
        echo "."
    ;;
  restart)
        echo -n "Restarting daemon: "$NAME
    start-stop-daemon --stop --quiet --oknodo --retry 30 --pidfile $PIDFILE
    start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- $DAEMON_OPTS
    echo "."
    ;;

  *)
    echo "Usage: "$1" {start|stop|restart}"
    exit 1
esac

exit 0

Then I create an installation for the above configuration:

install.sh

#!/usr/bin/env bash

echo "create a test service ..."
cp test.sh /etc/init.d/test
cp test.service /etc/systemd/system
chmod +x /etc/init.d/test
# sed -i "s/Your_User_Name/you_path/g" /etc/init.d/test
echo "created the test service"

Finally, do:

Set the access permission to your_script.py file:

$ chmod 755 <your_script.py>

Then install the service with:

$ sudo bash ./install.sh

Then trigger the service with systemctl or restart your machine if needed.

Then start your service:

$ sudo service test start

You can check its status:

$ sudo service test status

[NOTE]:


Benyamin Jafari
  • 27,880
  • 26
  • 135
  • 150
  • 1
    this does answer the question "running python script as a startup service" but not "... as a systemd service". The question may suffer from the XY problem, but IMHO, it is too soon to divert the topic. – MariusSiuram Jul 10 '18 at 09:10
  • 5
    Voting down because: The problem in the OP had nothing to do with finding python, so env python was extranious. Using an init.d-style script with systemd is worse style than the original post. Using cp rather than install to install executables is bad style. It's not as bad for scripts as it is for actual compiled programs, but using install would be better. – Sam Hartman Jul 12 '18 at 19:19
  • @SamHartman That shebang is important, without that there was an error in `service test status`. I answered to this question that works well, if you have an answer please put it on. – Benyamin Jafari Jul 12 '18 at 19:29
  • 1
    @Benyamin Jafari I voted to close. I don't think it's appropriate to answer and vote to close on the same question. – Sam Hartman Jul 12 '18 at 21:32
1

maybe it helps to add a Working directory at myscript.service:

[Service]
(...)
WorkingDirectory=/home/pi/your_working_directory

Best Regards Kilian

Kilian
  • 53
  • 5