You are not logged in.

#1 2018-10-30 06:26:56

krish.raghuram
Member
Registered: 2018-10-30
Posts: 4

Restarting arch systemd user service from within crontab

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.target

I 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.service

And ran it from within test's crontab, as,

* * * * * /bin/bash /home/test/test.sh

But I keep getting this error, and I'm unable to figure out the reason.

Failed to connect to bus: No such file or directory

I 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

#2 2018-10-30 06:29:57

ngoonee
Forum Fellow
From: Between Thailand and Singapore
Registered: 2009-03-17
Posts: 7,362

Re: Restarting arch systemd user service from within crontab

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

#3 2018-10-30 07:09:44

krish.raghuram
Member
Registered: 2018-10-30
Posts: 4

Re: Restarting arch systemd user service from within crontab

ngoonee wrote:

You probably need DBUS_SESSION_BUS_ADDRESS set correctly in the script's env. Why not just use systemd timer units?

DBUS_SESSION_BUS_ADDRESS

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?


Systemd Timers

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

#4 2018-10-30 09:55:14

berbae
Member
From: France
Registered: 2007-02-12
Posts: 1,304

Re: Restarting arch systemd user service from within crontab

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.sh

with test.sh being executable.

These are just ideas (not tested).

Offline

#5 2018-10-30 11:30:33

krish.raghuram
Member
Registered: 2018-10-30
Posts: 4

Re: Restarting arch systemd user service from within crontab

berbae wrote:

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.sh

with test.sh being executable.

These are just ideas (not tested).

Thanks for the ideas smile , 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

#6 2018-10-30 13:53:27

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,534
Website

Re: Restarting arch systemd user service from within crontab

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.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#7 2018-10-30 18:11:49

krish.raghuram
Member
Registered: 2018-10-30
Posts: 4

Re: Restarting arch systemd user service from within crontab

Trilby wrote:
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

Board footer

Powered by FluxBB