Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

Starting a gui app with systemd

Linux Kernel, Network, and Services configuration.
Post Reply
Message
Author
bedtime
Posts: 146
Joined: 2012-12-16 19:34
Has thanked: 1 time
Been thanked: 6 times

Starting a gui app with systemd

#1 Post by bedtime »

I would like systemd to start my status bar. The reason is that I would like the status bar to be able to work in various window managers. I've yet to get this to work properly. My guess would be that it is attempting to start the app before Xorg starts, but I don't know how to fix that. It seems to start fine manually. Also, I cannot seem to get the urxvtd daemon to run either.

Here is what I have so far:

~/.config/systemd/user/statusbar.service

Code: Select all

[Unit]
Description=Status bar

[Service]
Type=oneshot
Environment="DISPLAY=:0"
ExecStart=/home/user/scripts/./status.sh

[Install]
WantedBy=graphical.target

Code: Select all

$ systemctl --user                                                                                                 
UNIT                                                   LOAD   ACTIVE SUB       DESCRIPTION                         
sys-devices-pci0000:00-0000:00:1f.3-sound-card0.device loaded active plugged   200 Series PCH HD Audio             
-.mount                                                loaded active mounted   Root Mount                          
dev-hugepages.mount                                    loaded active mounted   /dev/hugepages                      
dev-mqueue.mount                                       loaded active mounted   /dev/mqueue                         
home.mount                                             loaded active mounted   /home                               
run-user-1000.mount                                    loaded active mounted   /run/user/1000                      
sys-kernel-debug.mount                                 loaded active mounted   /sys/kernel/debug                   
tmp.mount                                              loaded active mounted   /tmp                                
var.mount                                              loaded active mounted   /var                                
init.scope                                             loaded active running   System and Service Manager          
dbus.service                                           loaded active running   D-Bus User Message Bus              
-.slice                                                loaded active active    Root Slice                          
dbus.socket                                            loaded active running   D-Bus User Message Bus Socket       
dirmngr.socket                                         loaded active listening GnuPG network certificate management 
gpg-agent-browser.socket                               loaded active listening GnuPG cryptographic agent and passphr
gpg-agent-extra.socket                                 loaded active listening GnuPG cryptographic agent and passphr
gpg-agent-ssh.socket                                   loaded active listening GnuPG cryptographic agent (ssh-agent 
gpg-agent.socket                                       loaded active listening GnuPG cryptographic agent and passphr
dev-sda6.swap                                          loaded active active    /dev/sda6                           
basic.target                                           loaded active active    Basic System                        
default.target                                         loaded active active    Default                             
paths.target                                           loaded active active    Paths                               
sockets.target                                         loaded active active    Sockets                             
timers.target                                          loaded active active    Timers                              
mbsync.timer                                           loaded active waiting   Mailbox synchronization timer       

Code: Select all

systemctl --user status statusbar.service                                                                        
● statusbar.service - Status bar
   Loaded: loaded (/home/user/.config/systemd/user/statusbar.service; enabled; vendor preset: enabled)
   Active: inactive (dead)

Any ideas?

User avatar
bw123
Posts: 4015
Joined: 2011-05-09 06:02
Has thanked: 1 time
Been thanked: 28 times

Re: Starting a gui app with systemd

#2 Post by bw123 »

https://duckduckgo.com/html/?q=Starting ... th+systemd

NOT my preferred way, but you can do it your way.
...
I would like systemd to start my status bar. The reason is that I would like the status bar to be able to work in various window managers. I've yet to get this to work properly.
...
This is terribly vague, what do you mean by not working properly? I have noticed several of your posts are similar to this one, a vague problem and a vague description.

please try to do better?
resigned by AI ChatGPT

User avatar
GarryRicketson
Posts: 5644
Joined: 2015-01-20 22:16
Location: Durango, Mexico

Re: Starting a gui app with systemd

#3 Post by GarryRicketson »

by bw123 »This is terribly vague,
Is putting it mildly, I am not sure what the OP means by "status bar" either,.
The only thing for Debian I could find doing a search is this :
https://github.com/geommer/yabar
Debian
Yabar is available in the official repositories since Debian Stretch (9.0):
To vaque: Starting a gui app with systemd,
Which GUI app ? specifically , there are many, over 1000, .....
The reason is that I would like the status bar to be able to work in various window managers.
What "status bar" is it "yabar" or some other, ? I am sure there are others,
if it is not "yabar", what is it, and where did you get it ?
"various window managers", which ones, precisely ?
For example, for "yabar" they only mention :
Description
Yabar is a modern and lightweight status bar that is intended to be used along with minimal X window managers like bspwm and i3. Yabar has the following features:---snip---
So it might not be suitable for other window managers, but we have no clue as to what other window managers the OP is using,....
I am not sure, but if a "status bar" is what I think it is, it seems like that is something that would need to be started after the window manager is started,
and it would be part of the window managers configuration, to include starting a "status bar", so this is something that would be configured in each window manager, but it is not clear to me what the OP means by "status bar".

bedtime
Posts: 146
Joined: 2012-12-16 19:34
Has thanked: 1 time
Been thanked: 6 times

Re: Starting a gui app with systemd

#4 Post by bedtime »

bw123 wrote:This is terribly vague, what do you mean by not working properly? I have noticed several of your posts are similar to this one, a vague problem and a vague description.

please try to do better?
Sorry about that; over the years, I've found that when I post with lots of detail, my posts simply don't get answered. My guess is that they overwhelm the community and so get put aside. Obviously, this is all just speculation. :wink:

My issue:

The statusbar is dzen2 and the window manager is i3. My goal is to have a lightweight system that is mostly run by scripts and which is very extensible; I want to easily be able to install another OS in / and keep /home the same and just have things run, without all the hassle of reediting a bunch of config files—I'm just tired of doing this so many times. The reason I don't just start the statusbar (dzen2 script) in i3's ~/.config/i3/config file is that I might want to try another window manager, and I'd like this setup to work in all them.

I've done lots and lots of searching on the net—lots of reading tutorials, trying, fiddling. I've just come to the conclusion that systemd is not the best application for autostarting such a script; so, I just placed the dzen2 script in .xsession, and it seems to be working just fine. I'm happy with that.

But, on the subject of systemd, there is something that I would like to get working correctly: my mysync app. This periodically uses mbsync to download email, and so far it works fine, but with a small hack.

The hack is that I need to have the timer wait 3 seconds before it starts or else systemd will give an error that it was not able to connect to the host of my email server—likely due to systemd running the service before the net connection was made. I would like this application to start only once there is a solid internet connection (and not just after being in the process of starting the network).

~/.config/systemd/user/mbsync.service:

Code: Select all

[Unit]
Description=Mailbox synchronization service

[Service]
Type=oneshot
ExecStart=/usr/bin/mbsync -q user
~/.config/systemd/user/mbsync.timer:

Code: Select all

[Unit]
Description=Mailbox synchronization timer
After=network.target network-online.target dbus.socket
Requires=network.target network-online.target dbus.socket

[Timer]
OnBootSec=3s
OnUnitActiveSec=1m
Unit=mbsync.service

[Install]
WantedBy=timers.target
Here is the status when OnBootSec=0s:

Code: Select all

$ systemctl --user status mbsync.service                                                                           
● mbsync.service - Mailbox synchronization service
   Loaded: loaded (/home/user/.config/systemd/user/mbsync.service; static; vendor preset: enabled)
   Active: failed (Result: exit-code) since Mon 2018-07-23 08:03:57 EDT; 24s ago
  Process: 485 ExecStart=/usr/bin/mbsync -q user (code=exited, status=1/FAILURE)
 Main PID: 485 (code=exited, status=1/FAILURE)

Jul 23 08:03:57 debian systemd[449]: Starting Mailbox synchronization service...
Jul 23 08:03:57 debian mbsync[485]: Error: Cannot resolve server 'mail.autistici.org': Name or service not known
Jul 23 08:03:57 debian systemd[449]: mbsync.service: Main process exited, code=exited, status=1/FAILURE
Jul 23 08:03:57 debian systemd[449]: mbsync.service: Failed with result 'exit-code'.
Jul 23 08:03:57 debian systemd[449]: Failed to start Mailbox synchronization service.
$ systemctl --user status mbsync.timer                                                                             
● mbsync.timer - Mailbox synchronization timer
   Loaded: loaded (/home/user/.config/systemd/user/mbsync.timer; enabled; vendor preset: enabled)
   Active: active (waiting) since Mon 2018-07-23 08:03:57 EDT; 32s ago
  Trigger: Mon 2018-07-23 08:04:57 EDT; 27s left

Jul 23 08:03:57 debian systemd[449]: Started Mailbox synchronization timer.

The above is mainly taken from https://wiki.archlinux.org/index.php/Isync and gently modified. I didn't want to wait 2 minutes to download the email as I want the email downloaded ASAP (so it'll be there for other apps which will notify me). But I found that putting the OnBootSec setting to 0s, leads to the email host not being found, so I have to wait three seconds.

How to have the service start only after an internet connection has been made?

arzgi
Posts: 1185
Joined: 2008-02-21 17:03
Location: Finland
Been thanked: 31 times

Re: Starting a gui app with systemd

#5 Post by arzgi »

bedtime wrote:
But, on the subject of systemd, there is something that I would like to get working correctly: my mysync app. This periodically uses mbsync to download email, and so far it works fine, but with a small hack.

The hack is that I need to have the timer wait 3 seconds before it starts or else systemd will give an error that it was not able to connect to the host of my email server—likely due to systemd running the service before the net connection was made. I would like this application to start only once there is a solid internet connection (and not just after being in the process of starting the network).

How to have the service start only after an internet connection has been made?
I've used getmail to fecth my mail for many years. This script has worked as long, before systemd and during systemd, idea is to fetch mail only if online:

Code: Select all

#!/bin/bash

wget -q --tries=5 --timeout=10 --spider http://google.com
if [[ $? -eq 0 ]]; then
        /usr/bin/getmails
fi
And I run this script every 5 minutes, this I've set in crontab:

Code: Select all

*/5    *    *    *   *  /home/$USER/bin/script.sh -u $USER > /dev/null  2>&1

bedtime
Posts: 146
Joined: 2012-12-16 19:34
Has thanked: 1 time
Been thanked: 6 times

Re: Starting a gui app with systemd

#6 Post by bedtime »

arzgi wrote:I've used getmail to fecth my mail for many years. This script has worked as long, before systemd and during systemd, idea is to fetch mail only if online:

Code: Select all

#!/bin/bash

wget -q --tries=5 --timeout=10 --spider http://google.com
if [[ $? -eq 0 ]]; then
        /usr/bin/getmails
fi
And I run this script every 5 minutes, this I've set in crontab:

Code: Select all

*/5    *    *    *   *  /home/$USER/bin/script.sh -u $USER > /dev/null  2>&1
Thanks.

Cron won't work as it is not able to run immediately and then at interval; it must always wait that interval at the start.

I decided to just load this script within the statusbar command:

Code: Select all

#!/bin/sh

echo "Checking connection..."

online=1
while [ $online != 0 ]
do
	nc -z 8.8.8.8 53  >/dev/null 2>&1
	online=$?	# nc will return a '0' if a connection is made

	# Don't dilly dally if there is a connection
	if [ $online != 0 ] ; then sleep .1; fi
done

echo "Connected!"
echo "Syncing emails..."

mbsync -q user

echo "Done syncing emails!"

arzgi
Posts: 1185
Joined: 2008-02-21 17:03
Location: Finland
Been thanked: 31 times

Re: Starting a gui app with systemd

#7 Post by arzgi »

bedtime wrote:
Cron won't work as it is not able to run immediately and then at interval; it must always wait that interval at the start.
Cron accepts fractions of minutes, you could run it even every second, but that would cause high load. Not in this case, because the script had delay.

There is also anacron, I have used it only to to start things minutes after boot, I guess it accepts seconds too.

But you found your own solution, that's great.

User avatar
pylkko
Posts: 1802
Joined: 2014-11-06 19:02

Re: Starting a gui app with systemd

#8 Post by pylkko »

I personally don't want to use cron or anacron for anything any more, due to the facts that 1) - as far as I know - they are truly using systemd in the background and are only left there for compatibility reasons 2) systemd services give significantly better time management options, you can start a service on a specific minute, minutes after something, on Wednesdays, on every Wednesday that is even, the first time the machine is booted after a Wednesday in a month etc.

You do not seem to really indicate what the problems is, you post some output but these do not indicate any problem. Your services status says "dead" which is correct as a oneshot service always exits and dies after doing it's task. But I think I can guess, the bar doesn't load?

Starting things with a systemd service gets complicated when you are not running a simple system background service or daemon; when you need the user's environment, need to access their key ring or other such things. For most graphical programs it is just easier to auto-start them with other ways, for example in the xinitrc file before starting the window manager.

At this time you should try to read the logs and see what went wrong. It could be that the service tries to run the scrip too early, or it could be that it tries it in a way that is missing some other relevant thing, like credentials, maybe you have the wrong display, maybe who knows what...

arzgi
Posts: 1185
Joined: 2008-02-21 17:03
Location: Finland
Been thanked: 31 times

Re: Starting a gui app with systemd

#9 Post by arzgi »

pylkko wrote:I personally don't want to use cron or anacron for anything any more, due to the facts that 1) - as far as I know - they are truly using systemd in the background and are only left there for compatibility reasons 2) systemd services give significantly better time management options, you can start a service on a specific minute, minutes after something, on Wednesdays, on every Wednesday that is even, the first time the machine is booted after a Wednesday in a month etc.
I don't doubt your words, but can you give some reference of 1) that cron uses systemd in backroung.

User avatar
pylkko
Posts: 1802
Joined: 2014-11-06 19:02

Re: Starting a gui app with systemd

#10 Post by pylkko »

Yeah I don't actually know that for sure, I know that you can use it like that, but I have not checked if default Debian installs do. But if they don't and they have another real implementation of cron (like vixie) then I would suspect that to be performance wise dumb in addition to being redundant. But I am not at a Debian machine now (because work place uses Ubuntu).

https://github.com/systemd-cron/systemd-cron

https://manpages.debian.org/stretch/sys ... .5.en.html

https://manpages.debian.org/stretch/sys ... .5.en.html

arzgi
Posts: 1185
Joined: 2008-02-21 17:03
Location: Finland
Been thanked: 31 times

Re: Starting a gui app with systemd

#11 Post by arzgi »

pylkko wrote:Yeah I don't actually know that for sure, I know that you can use it like that, but I have not checked if default Debian installs do. But if they don't and they have another real implementation of cron (like vixie) then I would suspect that to be performance wise dumb in addition to being redundant. But I am not at a Debian machine now (because work place uses Ubuntu).

https://github.com/systemd-cron/systemd-cron

https://manpages.debian.org/stretch/sys ... .5.en.html

https://manpages.debian.org/stretch/sys ... .5.en.html
Thanks for replying! I've read cron and anacon man pages, but those I did not think, thanks!

This was just out of interest, I feel secure, because only have that one cronjob, and have read about systemd so I know how to replace it.

User avatar
bw123
Posts: 4015
Joined: 2011-05-09 06:02
Has thanked: 1 time
Been thanked: 28 times

Re: Starting a gui app with systemd

#12 Post by bw123 »

arzgi wrote: ...

I don't doubt your words, but can you give some reference of 1) that cron uses systemd in backroung.
look here on a debian system:
/etc/cron.d/anacron

what does this part do?

Code: Select all

30 7    * * *   root    [ -x /etc/init.d/anacron ] && if [ ! -d /run/systemd/system ]; then /usr/sbin/invoke-rc.d anacron start >/dev/null; fi
I think it's true that systemd timers are better, but not really sure that both won't work on the same system.
resigned by AI ChatGPT

User avatar
pylkko
Posts: 1802
Joined: 2014-11-06 19:02

Re: Starting a gui app with systemd

#13 Post by pylkko »

I think the whole point of having a cron implementation or the capacity to parse cron syntax in systemd is so that users can use either method and old crontabs remain usable and portable. Systemd timers wont of course work on systemdless machines.

arzgi
Posts: 1185
Joined: 2008-02-21 17:03
Location: Finland
Been thanked: 31 times

Re: Starting a gui app with systemd

#14 Post by arzgi »

bw123 wrote:
arzgi wrote: ...

I don't doubt your words, but can you give some reference of 1) that cron uses systemd in backroung.
look here on a debian system:
/etc/cron.d/anacron

what does this part do?

Code: Select all

30 7    * * *   root    [ -x /etc/init.d/anacron ] && if [ ! -d /run/systemd/system ]; then /usr/sbin/invoke-rc.d anacron start >/dev/null; fi
I think it's true that systemd timers are better, but not really sure that both won't work on the same system.
That was even mentioned in /usr/share/doc/anacron/some file, that anacron normally does not run on battery, but if configured so, it relys on systemd-timer.

arzgi
Posts: 1185
Joined: 2008-02-21 17:03
Location: Finland
Been thanked: 31 times

Re: Starting a gui app with systemd

#15 Post by arzgi »

Code: Select all

$ aptitude search systemd | grep cron
p  systemd-cron - systemd units to provide cron daemon & anacron functionality
$ aptitude show systemd-cron
Packet: systemd-cron                      
Version: 1.5.8-1
Status: not installed 
Provides: anacron, cron-daemon
Description: systemd units to provide cron daemon & anacron functionality
...
$ aptitude why cron
i   logrotate Depends cron | anacron | cron-daemon
$ aptitude why logrotate
'i   rsyslog Recommends logrotate
$ 
So it seems, currently admin can choose cron, or systemd-cron. Anacron when installed, replaces cron with systemd-cron.

User avatar
debiman
Posts: 3063
Joined: 2013-03-12 07:18

Re: Starting a gui app with systemd

#16 Post by debiman »

bedtime, where are you with the original problem?
it seems the thread has meandered away, although the original problem is by no means unsolvable...

a few thoughts:
- graphical.target might not mean that Xorg is actually up and running
- your script needs to generate some debugging output, later accessible via systemctl status ...
- it might require more env variables
- does the script work started manually? actually, show us the whole script.
- i remember that systemd/user used to be tricky. read the wiki.

and what has fetching mail got to do with the original problem?

it's confusing.

Post Reply