Summary:
A python script using apshceduler fails to load as service with exit code 203/EXEC. Important to note that same script designed to run every 10 seconds using while True: syntax runs very well as daemon (service).
Details
I wrote a simple test.py program. It appends some a.txt file every 10 seconds with "some" string. When I try to run it as daemon service as it comes out with error code. . In itself without using it as daemon (service) it works fine. Also if I don't use apscheduler in writing the test.py file then the service runs smoothly.
I will put all the code here for details including the systemctl status
Code
test.py file
import os, glob, shutil
from datetime import datetime
from apscheduler.schedulers.blocking import BlockingScheduler
sched = BlockingScheduler()
def test():
appendFile = open(r'/home/sangharsh/code/a.txt', 'a')
appendFile.write("Jai Bhim \n" )
appendFile.close()
sched.add_job(test, 'interval', seconds=10)
sched.start()
Its located # - /home/sangharsh/code/workingWithFiles/
To run it as daemon I create service file -
service config file
[Unit]
Description=Test Service
After=multi-user.target
Conflicts=getty#tty1.service
[Service]
Type=simple
ExecStart=/usr/bin/env python3 /home/sangharsh/code/workingWithFiles/test.py
StandardInput=tty-force
[Install]
WantedBy=multi-user.target
This file is located # - /lib/systemd/system/
I restart the daemon-
sudo systemctl daemon-reload
Then enable the test.service file -
sudo systemctl enable test.service
and to start the test.service
sudo systemctl start test.service
and to scheck the status
sudo systemctl status test.service
The systemctl status shows:
● test.service - Test Service
Loaded: loaded (/lib/systemd/system/test.service; enabled; vendor preset: ena
Active: failed (Result: exit-code) since Tue 2019-07-09 23:28:10 IST; 13s ago
Process: 5272 ExecStart=/usr/bin/env python3 /home/sangharsh/code/workingWithF
Main PID: 5272 (code=exited, status=1/FAILURE)
Jul 09 23:28:10 sangharsh-HP-240-G4-Notebook-PC systemd[1]: Started Test Service
Jul 09 23:28:10 sangharsh-HP-240-G4-Notebook-PC systemd[1]: test.service: Main p
Jul 09 23:28:10 sangharsh-HP-240-G4-Notebook-PC systemd[1]: test.service: Failed
I referred this question. I tried the options but it doesn't help.
And this is not much applicable as it has permission issues.
1: https://unix.stackexchange.com/q/472950/361166 ```
Please guide.
Related
I have listed below the steps as I entered them. I cannot get the Pi (Raspbian GNU/Linux Buster (10)) to launch alarm.service after startup. When I log into the Pi and launch alarm.service manually from the GUI it works, as does the script it runs -- (The script is written to loop) Only problem is I can't get alarm.service to launch automatically without the GUI. Be great to have it launch last after all other services are up and running.
1. sudo nano /home/pi/alarm_service.py - (fyi: this script runs nicely when i launch it manually from command prompt after pi has booted and i am logged in)
2. sudo nano /etc/systemd/system/alarm.service
[Unit]
Description=Alarm Service
After=multi-user.target
Conflicts=getty#tty1.service
[Service]
Type=simple
ExecStart=/home/pi/alarm_service.py
StandardInput=tty-force
[Install]
WantedBy=multi-user.target
3. sudo chmod 644 /etc/systemd/system/alarm.service
4. sudo systemctl daemon-reload
5. sudo systemctl enable alarm.service
6. sudo systemctl start alarm.service
Error log:
alarm.service - Alarm service Loaded: loaded (/etc/systemd/system/alarm.service; enabled; vendor preset: enabled) Active: failed (Result: exit-code) since Fri 2021-05-14 18:57:24 AEST; 1min 18s ago Process: 501 ExecStart=/home/pi/alarm_service.py (code=exited, status=1/FAILURE) Main PID: 501 (code=exited, status=1/FAILURE)
May 14 18:57:24 raspberrypi alarm_service.py[501]: File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 10 May 14 18:57:24 raspberrypi alarm_service.py[501]: sock = self._create_socket_connection() May 14 18:57:24 raspberrypi alarm_service.py[501]: File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 35 May 14 18:57:24 raspberrypi alarm_service.py[501]: return socket.create_connection(addr, source_address=source, timeout=sel May 14 18:57:24 raspberrypi alarm_service.py[501]: File "/usr/lib/python2.7/socket.py", line 575, in create_connection May 14 18:57:24 raspberrypi alarm_service.py[501]: raise err May 14 18:57:24 raspberrypi alarm_service.py[501]: socket.error: [Errno 101] Network is unreachable May 14 18:57:24 raspberrypi systemd[1]: alarm.service: Main process exited, code=exited, status=1/FAILURE May 14 18:57:24 raspberrypi systemd[1]: alarm.service: Failed with result 'exit-code'. May 14 18:57:24 raspberrypi systemd[1]: Failed to start Alarm service.
The principle I follow is to make the environment that systemd creates for the script to run as similar to what I use to develop it as possible.
For the sake of testing I just ran shutdown -r now on a VM where I do this exact thing. On boot, voila.
$ systemctl status mypython
● mypython.service
Loaded: ...etc.
The file I have set is
less /etc/systemd/system/multi-user.target.wants/mypython.service
Content
[Unit]
Description=mypython
[Service]
WorkingDirectory=/home/me
ExecStart=/usr/bin/python3 /home/me/mypython.py ./config.yaml
Restart=always
RestartSec=5
User=me
Group=me
[Install]
WantedBy=multi-user.target
I have a reminder app and i want to run it as a service. I created a service file called 'reminder_py.service' under '/lib/systemd/system'. commands inside 'reminder_py.service':
[Unit]
Description=Dummy Service
After=multi-user.target
Conflicts=getty#tty1.service
[Service]
Type=simple
Username=hrx
Groupname=hrx
ExecStart=/usr/bin/python3 /home/hrx/reminder/reminder.py
StandardInput=tty-force
[Install]
WantedBy=multi-user.target
And I tried to start it with the following commands:
sudo systemctl daemon-reload
sudo systemctl enable reminder_py.service
sudo systemctl start reminder_py.service
But the sudo systemctl status reminder_py.service command says:
hrx#X230:/lib/systemd/system$ sudo systemctl status reminder_py.service
● reminder_py.service - Dummy Service
Loaded: loaded (/lib/systemd/system/reminder_py.service; enabled; vendor p>
Active: failed (Result: exit-code) since Sat 2020-08-15 23:59:06 +03; 2min>
Process: 13952 ExecStart=/usr/bin/python3 /home/hrx/reminder/reminder.py (c>
Main PID: 13952 (code=exited, status=2)
Aug 15 23:59:06 X230 systemd[1]: Started Dummy Service.
Aug 15 23:59:06 X230 systemd[1]: reminder_py.service: Main process exited, code>
Aug 15 23:59:06 X230 systemd[1]: reminder_py.service: Failed with result 'exit->
How can I solve this problem?
Maybe the following can help:
hrx#X230:/lib/systemd/system$ users
hrx
hrx#X230:/lib/systemd/system$ groups
hrx adm cdrom sudo dip plugdev lpadmin sambashare
hrx#X230:/lib/systemd/system$ ls -al | grep reminder_py.service
-rwxrwxrwx 1 root root 256 Aug 15 23:55 reminder_py.service
And I found a comment from another site that could help me. But I don't quite understand what he's saying.
A fine and simple to follow tutorial. I would add a couple of caveats – with the .service file as written the python script will be run as root and this may have unintended consequences. Also the environment will be different from that for a normal user. To fix add the lines User=username and Group=groupname before the ExecStart line. To add environment variables expected by the script add the line Environment=”variable_name=variable_value” before the ExecStart line.
I solved the problem by replacing contents of the file with:
[Unit]
Description=Reminder App
After=multi-user.target
Conflicts=getty#tty1.service
[Service]
Type=simple
ExecStart=/home/hrx/anaconda3/bin/python /home/hrx/Desktop/reminder/reminder.py
StandardInput=tty-force
[Install]
WantedBy=multi-user.target
To learn the path of python run this command:
type -a python
I followed this tutorial
https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-centos-7
Tried to deploy Django project on centos 8
Everything went fine and worked, except gunicorn.service
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=facealatoo
Group=nginx
WorkingDirectory=/home/facealatoo/nadyr/promed
ExecStart=/home/facealatoo/nadyr/promed/venv/bin/gunicorn \
--workers 3 \
--bind unix:/home/facealatoo/nadyr/promed/promed.sock \
configs.wsgi:application
[Install]
WantedBy=multi-user.target
Folders destinations
my project folder destination '/home/facealatoo/nadyr/promed' settings.py file 'home/facealatoo/nadyr/promed/configs/settings.py'
server user name 'facealatoo'
after running
sudo systemctl daemon-reload
sudo systemctl start gunicorn
sudo systemctl enable gunicorn
sudo systemctl status gunicorn.service
Error message
● gunicorn.service - gunicorn daemon
Loaded: loaded (/etc/systemd/system/gunicorn.service; enabled; vendor
preset: disabled)
Active: failed (Result: exit-code) since Fri 2020-05-15 18:37:22 +06; 13s
ago
Main PID: 32293 (code=exited, status=203/EXEC)
May 15 18:37:22 facealatoo.net.kg systemd[1]: Started gunicorn daemon.
May 15 18:37:22 facealatoo.net.kg systemd[1]: gunicorn.service: Main
process exited, code=exited, status=203/EXEC
May 15 18:37:22 facealatoo.net.kg systemd[1]: gunicorn.service: Failed
with result 'exit-code'.
Please help me! ;) Thanks in advance ))))
I just change socket file destination (home/facealatoo(user)/) and gunicorn destination (usr/local/bin/gunicorn). And these actions solved my problem)))
i have read a lot of posts on this site on how to implement a python script as a service.
After fiddling around i am at the point that the service is started via systemctl (and running) but the script is doing nothing...
My config file in /etc/systemd/system/:
[Unit]
Description=tg Bot
[Service]
Type=simple
User=user
WorkingDirectory=/home/user/tg_onduty/
ExecStart=/usr/bin/python3 /home/user/tg_onduty/on_duty.py
Restart=always
[Install]
WantedBy=multi-user.target
Output:
user#server:~$ sudo service tg_onduty status
● tg_onduty.service - Telegram OnDuty Bot
Loaded: loaded (/etc/systemd/system/tg_onduty.service; enabled; vendor preset
Active: active (running) since Thu 2018-02-15 11:28:20 CET; 2min 17s ago
Main PID: 1538 (python3)
Tasks: 9
Memory: 17.7M
CPU: 351ms
CGroup: /system.slice/tg_onduty.service
└─1538 /usr/bin/python3 /home/user/tg_onduty/on_duty.py
I have read https://unix.stackexchange.com/questions/339638/difference-between-systemd-and-terminal-starting-program/339645#339645 and understand that running the script via systemctl is different than running via CLI (via CLI /usr/bin/python3 /home/user/tg_onduty/on_duty.py is working).
My question is now:
How can i trace or see whats going wrong or why the script seems to do nothing?
Via Journalctl i only see: Feb 15 11:56:17 server systemd[1]: Started tg Bot.
Any help is appreciated.
Thanks,
David
Are you sure you script not running at all? try to put logs in your script to see if something inside the script doesn't work.
Add a simple action to the script. maybe echo something or create a dummy file. That is the easiest way to know if it's working
I have Python script with works as a systemd daemon in CentOS 7. The daemon gets executed by the version of python I created in a virtualenv. I am trying to tweak the script to be able to set the virtualenv path in an environment variable so I can easily switch to a different virtualenv by changing paths via the one variable and restarting the service. I have created my systemd scripts to be able to initialize multiple instances of the daemon and this works great. When I try to use an environment variable to point to my python parser things break. Here is what I have so far.
/etc/systemd/system/pipeline-remove#.service:
[Unit]
Description=pipeline remove tickets worker instances as a service, instance %i
Requires=pipeline-remove.service
Before=pipeline-remove.service
BindsTo=pipeline-remove.service
[Service]
PermissionsStartOnly=true
Type=idle
User=root
ExecStart=/path/to/venv/bin/python /pipeline/python/daemons/remove_tickets.py
Restart=always
TimeoutStartSec=10
RestartSec=10
[Install]
WantedBy=pipeline-remove.service
/etc/systemd/system/pipeline-remove.service (to start all instances):
[Unit]
Description=manages pipeline remove tickets worker instances as a service, instance
[Service]
Type=oneshot
ExecStart=/usr/bin/sh /usr/bin/pipeline-remove-start.sh
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
pipeline-remove-start.sh:
#!/bin/bash
systemctl start pipeline-remove#{1..2}
This works great for me, but things break when I try to set the python dir the following way:
/etc/profile.d/pipeline_envvars.sh:
PIPELINE_VIRTUALENV=/path/to/venv
/etc/systemd/system/pipeline-remove#.service:
[Unit]
Description=pipeline remove tickets worker instances as a service, instance %i
Requires=pipeline-remove.service
Before=pipeline-remove.service
BindsTo=pipeline-remove.service
[Service]
PermissionsStartOnly=true
Type=idle
User=root
EnvironmentFile=/etc/profile.d/pipeline_envvars.sh
ExecStart=/${PIPELINE_VIRTUALENV}/bin/python /pipeline/python/daemons/remove_tickets.py
Restart=always
TimeoutStartSec=10
RestartSec=10
[Install]
WantedBy=pipeline-remove.service
I then try to start it:
sudo systemctl daemon-reload
sudo systemctl restart pipeline-remove#{1..1}
sudo systemctl status pipeline-remove#{1..1}
The status shows the following exit code 203 which means executable not found:
● pipeline-remove#1.service - pipeline remove tickets worker instances as a service, instance 1
Loaded: loaded (/etc/systemd/system/pipeline-remove#.service; disabled; vendor preset: disabled)
Active: activating (auto-restart) (Result: exit-code) since Fri 2018-01-26 15:04:50 UTC; 6s ago
Process: 11716 ExecStart=/${PIPELINE_VIRTUALENV}/bin/python /pipeline/python/daemons/remove_tickets.py (code=exited, status=203/EXEC)
Main PID: 11716 (code=exited, status=203/EXEC)
Jan 26 15:04:50 dev systemd[1]: pipeline-remove#1.service: main process exited, code=exited, status=203/EXEC
Jan 26 15:04:50 dev systemd[1]: Unit pipeline-remove#1.service entered failed state.
Jan 26 15:04:50 dev systemd[1]: pipeline-remove#1.service failed.
Found the exec code here. Also found this in syslog, /var/log/messages:
Jan 26 15:07:13 dev systemd: Starting pipeline remove tickets worker instances as a service, instance 1...
Jan 26 15:07:13 dev systemd: Failed at step EXEC spawning /${PIPELINE_VIRTUALENV}/bin/python: No such file or directory
Jan 26 15:07:13 dev systemd: pipeline-remove#1.service: main process exited, code=exited, status=203/EXEC
Jan 26 15:07:13 dev systemd: Unit pipeline-remove#1.service entered failed state.
Jan 26 15:07:13 dev systemd: pipeline-remove#1.service failed.
Jan 26 15:07:23 dev systemd: pipeline-remove#1.service holdoff time over, scheduling restart.
Jan 26 15:07:23 dev systemd: Started pipeline remove tickets worker instances as a service, instance 1.
When I try to drop the leading / in ExecStart I get a relative path error even though my env var does contain an absolute path:
Failed to start pipeline-remove#1.service: Unit is not loaded properly:
Invalid argument.
See system logs and 'systemctl status pipeline-remove#1.service' for
details.
And status shows the following:
vagrant#dev:~$ sudo systemctl status pipeline-remove#{1..1}
● pipeline-remove#1.service - pipeline remove tickets worker instances as a service, instance 1
Loaded: error (Reason: Invalid argument)
Active: inactive (dead)
Jan 26 15:11:39 dev systemd[1]: pipeline-remove#1.service failed.
Jan 26 15:11:42 dev systemd[1]: Stopped pipeline remove tickets worker instances as a service, instance 1.
Jan 26 15:11:42 dev systemd[1]: [/etc/systemd/system/pipeline-remove#.service:12] Executable path is not absolute, ignoring: ${PIPELINE_VIRTUALENV}/bin/python /pipel...e_tickets.py
Jan 26 15:11:42 dev systemd[1]: pipeline-remove#1.service lacks both ExecStart= and ExecStop= setting. Refusing.
I used this guide to help me get started but now I'm stuck. How can I get my python daemon to come up while setting the python executable path from an environment variable?
After some more reading, I stumbled on the answer here. The problem is the first argument of ExecStart must be a literal:
ExecStart= Commands with their arguments that are executed when this
service is started. For each of the specified commands, the first
argument must be an absolute and literal path to an executable.
Reading on further on ExecStart it says:
Variables whose value is not known at expansion time are treated as
empty strings. Note that the first argument (i.e. the program to
execute) may not be a variable.
I also ended up stumbling across this answer which looks like the same problem. In the end this is what worked: wrapping the entire thing with shell to run:
[Unit]
Description=pipeline remove tickets worker instances as a service, instance %i
Requires=pipeline-remove.service
Before=pipeline-remove.service
BindsTo=pipeline-remove.service
[Service]
PermissionsStartOnly=true
Type=idle
User=root
EnvironmentFile=/etc/profile.d/pipeline_envvars.sh
ExecStart=/bin/sh -c '${PIPELINE_VIRTUALENV}/bin/python /pipeline/python/daemons/remove_tickets.py'
Restart=always
TimeoutStartSec=10
RestartSec=10
[Install]
WantedBy=pipeline-remove.service
So ExecStart=/bin/sh -c '<your command>' saves the day, I can now specify the python interpreter path for my environment variable. Will leave the answer up, hopefully, saves others some time.