You are not logged in.
I have a user test.
test has a systemd user service, located at /home/test/.config/systemd/user/testserv.service
The service file contains,
[Unit]
Description=Test Service
After=network.target
[Service]
WorkingDirectory=/home/test/test_website
ExecStart=/home/test/venv/bin/gunicorn \
--access-logfile \
- \
--workers 2 \
--bind unix:/home/test/test_website/test_website.sock run:app
[Install]
WantedBy=multi-user.targetI would like to restart this service from within cron.
For this, I created a script /home/test/test.sh
#!/bin/bash
PATH="/home/test/venv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
HOME="/home/test"
SHELL="/bin/bash"
systemctl --user restart testserv.serviceAnd ran it from within test's crontab, as,
* * * * * /bin/bash /home/test/test.shBut I keep getting this error, and I'm unable to figure out the reason.
Failed to connect to bus: No such file or directoryI read through this page : https://wiki.archlinux.org/index.php/Systemd/User
Also tried to see the difference in environment variables between normal shell and cron.
Could someone point me in the right direction?
Offline
You probably need DBUS_SESSION_BUS_ADDRESS set correctly in the script's env. Why not just use systemd timer units?
Allan-Volunteer on the (topic being discussed) mailn lists. You never get the people who matters attention on the forums.
jasonwryan-Installing Arch is a measure of your literacy. Maintaining Arch is a measure of your diligence. Contributing to Arch is a measure of your competence.
Griemak-Bleeding edge, not bleeding flat. Edge denotes falls will occur from time to time. Bring your own parachute.
Offline
You probably need DBUS_SESSION_BUS_ADDRESS set correctly in the script's env. Why not just use systemd timer units?
I looked up my environment for DBUS_SESSION_BUS_ADDRESS but there was no such variable.
But that gave me an idea. From the shell, I started "unsetting" each environment variable, and then tried to restart the service (to figure out which variable was needed).
When I unset XDG_RUNTIME_DIR, and tried to restart, I got the "Failed to connect to bus " error.
So I added XDG_RUNTIME_DIR="/run/user/1000" to the script and tried again.
Still got same error.
Im stuck at this point.
• Does systemctl need XDG_RUNTIME_DIR?
• If so, then why adding it to script is not working? Is there some other access restriction?
I prefer not to use systemd timers for this particular situation because,
• I also have other cron scripts which do tasks related to this, and I'd like to keep everything in one place.
• This "set of related tasks" is somewhat temporary, so I do not want to mess with systemd service for temporary works.
I apologize for being vague.
Offline
Hello
Try to add in the script
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
I presume the 'test' user id is 1000.
And if the cron task is owned by root, try to use
* * * * * /bin/su -l test /home/test/test.shwith test.sh being executable.
These are just ideas (not tested).
Offline
Hello
Try to add in the script
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
I presume the 'test' user id is 1000.
And if the cron task is owned by root, try to use
* * * * * /bin/su -l test /home/test/test.shwith test.sh being executable.
These are just ideas (not tested).
Thanks for the ideas
, but I still get the same error.
'test' user id is 1000.
The script is running from test user's crontab, not from root.
Offline
I prefer not to use systemd timers for this particular situation because,
• I also have other cron scripts which do tasks related to this, and I'd like to keep everything in one place.
But you're alreay failing this criteria as you are using a systemd service to start gunicorn. Why not just (re)start gunicorn from a cron task?
But more importantly, why to do you need to restart it on a timer? Is it crashing? There are many better ways of dealing with that.
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
krish.raghuram wrote:I prefer not to use systemd timers for this particular situation because,
• I also have other cron scripts which do tasks related to this, and I'd like to keep everything in one place.
But you're alreay failing this criteria as you are using a systemd service to start gunicorn. Why not just (re)start gunicorn from a cron task?
But more importantly, why to do you need to restart it on a timer? Is it crashing? There are many better ways of dealing with that.
It is not crashing... Guess I'll explain the whole situation...
The systemd service manages the flask application (which is a website).
I wanted to use cron as a lightweight continuous deployment solution.
The site will be live only for a few weeks (website is for university event), and so, I did not go for a full fledged CI/CD solution like Jenkins or Travis.
There is only one other cron script, and it does a git-pull at regular intervals.
But right now, I pursuing the problem mainly because I'm curious.
PS : thank you very much for the help
PPS : love your "signature"
Last edited by krish.raghuram (2018-10-30 18:14:14)
Offline