start a service only after Network is up and running

Kernels & Hardware, configuring network, installing services

start a service only after Network is up and running

Postby PsySc0rpi0n » 2020-03-01 16:52

Hello.

I need some guidance here.
I need to start 3 applications at boot time. Or better, I just want to start them automatically. It doesn't need to be at boot time. It may be after the desktop is loaded or whatever. The important is that they only run after the network is up and running.

These services are bitcoind (bitcoin full node), rtorrent and irssi.
I tried to start them in my .profile file but I was having issues because I end up sometimes not being able to login because I en up verifying that these services were messing up with my terminal. What happens is that if I try to start these applications manually the exact same way I have in .profile, within screen, the applications starts but I can't get the prompt back. The cursor stays blinking in terminal for ever and I have to close that terminal window and open another. So, when I reboot my laptop, I end up eventually hanged up after I enter my password in login manager and I can't get into my Desktop.

So, I'm trying to change the approach an instead of using .profile to start applications within screen, I was trying to use cron jobs. But there's another issue here. Network is probably down when cron tries to start them.

So, I'm now trying systemd that allows us to check for network status. But this brings another downside. I've been reading how to do this and seems that the way it can be done, might eventually slow down boot up time once that the boot only continues after all devices are up and with IPs assigned.

Anyway, I can bare with this downside because I don't intend to reboot that many times.

So, I need some guidance on how to achieve this.

For what I read so far, we need to create a service file under
Code: Select all
/etc/systemd/system


named:
Code: Select all
name.of.app.service


and inside it there must be 3 sections
Code: Select all
[Unit]
[Service]
[Install]


So, I need to make sure this is set how I think it is.
My goal is to launch irssi, bitcoind and rtorrent within screen, after my network is up and running.

The commands I usually run manually is:
Code: Select all
screen -S rtorrent -fa -D -m rtorrent
screen -S bitcoind -fa -D -m bitcoind
screen -S irssi -fa -D -m irssi


So, I need to create the following files:
Code: Select all
/etc/systemd/system/rtorrent.service
/etc/systemd/system/bitcoind.service
/etc/systemd/system/irssi.service


And inside each file:
Code: Select all
[Unit]
Description=rtorrent for torrent file sharing
Documentation=man:rtorrent
After=

[Service]
ExecStart=screen -S rtorrent -fa -D -m rtorrent

[Install]
(Not sure if anything is neeed/mandatory here)


Anyone can help?
Last edited by PsySc0rpi0n on 2020-03-06 21:47, edited 2 times in total.
User avatar
PsySc0rpi0n
 
Posts: 181
Joined: 2012-10-24 13:54
Location: Portugal

Re: start a service only after Network is up and running

Postby Head_on_a_Stick » 2020-03-01 17:03

PsySc0rpi0n wrote:I tried to start them in my .profile file but I was having issues because I end up sometimes not being able to login because I en up verifying that these services were messing up with my terminal. What happens is that if I try to start these applications manually the exact same way I have in .profile, within screen, the applications starts but I can't get the prompt back.

Add an ampersand after the commands to background them, like this:
Code: Select all
screen -S rtorrent -fa -D -m rtorrent &
screen -S bitcoind -fa -D -m bitcoind &
screen -S irssi -fa -D -m irssi &

https://bashitout.com/2013/05/18/Ampers ... -line.html
User avatar
Head_on_a_Stick
 
Posts: 11454
Joined: 2014-06-01 17:46
Location: /dev/chair

Re: start a service only after Network is up and running

Postby PsySc0rpi0n » 2020-03-01 17:55

Head_on_a_Stick wrote:
PsySc0rpi0n wrote:I tried to start them in my .profile file but I was having issues because I end up sometimes not being able to login because I en up verifying that these services were messing up with my terminal. What happens is that if I try to start these applications manually the exact same way I have in .profile, within screen, the applications starts but I can't get the prompt back.

Add an ampersand after the commands to background them, like this:
Code: Select all
screen -S rtorrent -fa -D -m rtorrent &
screen -S bitcoind -fa -D -m bitcoind &
screen -S irssi -fa -D -m irssi &

https://bashitout.com/2013/05/18/Ampers ... -line.html


That's not what I'm looking for now. I'm looking for the systemd solution.
But I'll still give it a try and see how it works.
User avatar
PsySc0rpi0n
 
Posts: 181
Joined: 2012-10-24 13:54
Location: Portugal

Re: start a service only after Network is up and running

Postby Head_on_a_Stick » 2020-03-01 18:02

PsySc0rpi0n wrote:That's not what I'm looking for now. I'm looking for the systemd solution.

You will need to create user services or the commands will be run as root.

The [Install] section declares for which .target the unit file is started, you will probably want to define your own to ensure correct startup.

See systemd.unit(5), systemd.service(5) & systemd.target(5) for more on this.

Good luck!
User avatar
Head_on_a_Stick
 
Posts: 11454
Joined: 2014-06-01 17:46
Location: /dev/chair

Re: start a service only after Network is up and running

Postby PsySc0rpi0n » 2020-03-01 18:38

Head_on_a_Stick wrote:
PsySc0rpi0n wrote:That's not what I'm looking for now. I'm looking for the systemd solution.

You will need to create user services or the commands will be run as root.

The [Install] section declares for which .target the unit file is started, you will probably want to define your own to ensure correct startup.

See systemd.unit(5), systemd.service(5) & systemd.target(5) for more on this.

Good luck!


I guess, by your words, I'm going to have an hard, time, right?

I tried this though, in /etc/systemd/system/bicoind.service, but this did nothing:
Not as root, not as normal user.
Code: Select all
[Unit]
Description=Start bitcoind after Network is available
Documentation=man:rtorrent
After=network-online.target

[Service]
ExecStart=/usr/binscreen -S bitcoind -fa -D -m /usr/local/bin/bitcoind

[Install]
WantedBy=network-online.target
User avatar
PsySc0rpi0n
 
Posts: 181
Joined: 2012-10-24 13:54
Location: Portugal

Re: start a service only after Network is up and running

Postby Head_on_a_Stick » 2020-03-01 19:43

PsySc0rpi0n wrote:I guess, by your words, I'm going to have an hard, time, right?

Let's not be negative. You're going to have a challenging time :)

PsySc0rpi0n wrote:Not as root, not as normal user.

And how did you do that, exactly?

I wouldn't ask but the surveillance cameras I installed at your home don't seem to be working... :mrgreen:
User avatar
Head_on_a_Stick
 
Posts: 11454
Joined: 2014-06-01 17:46
Location: /dev/chair

Re: start a service only after Network is up and running

Postby PsySc0rpi0n » 2020-03-01 20:00

Head_on_a_Stick wrote:
PsySc0rpi0n wrote:I guess, by your words, I'm going to have an hard, time, right?

Let's not be negative. You're going to have a challenging time :)

PsySc0rpi0n wrote:Not as root, not as normal user.

And how did you do that, exactly?

I wouldn't ask but the surveillance cameras I installed at your home don't seem to be working... :mrgreen:


If I understood your question, you mean how I checked that bitcoind is not running neither as my user or as root, right?

I ran htop and searched for bitcoind with my username, then I 'su' and did the same. Couldn't find any instance of bitcoind running!

I read from the mans you told me that I might need to create this file under one of these folders:
Code: Select all
User Unit Search Path
~/.config/systemd/user.control/*
$XDG_RUNTIME_DIR/systemd/user.control/*
$XDG_RUNTIME_DIR/systemd/transient/*
$XDG_RUNTIME_DIR/systemd/generator.early/*
~/.config/systemd/user/*
/etc/systemd/user/*
$XDG_RUNTIME_DIR/systemd/user/*
/run/systemd/user/*
$XDG_RUNTIME_DIR/systemd/generator/*
~/.local/share/systemd/user/*
...
/usr/lib/systemd/user/*
$XDG_RUNTIME_DIR/systemd/generator.late/*
Last edited by PsySc0rpi0n on 2020-03-01 20:06, edited 1 time in total.
User avatar
PsySc0rpi0n
 
Posts: 181
Joined: 2012-10-24 13:54
Location: Portugal

Re: start a service only after Network is up and running

Postby Head_on_a_Stick » 2020-03-01 20:04

PsySc0rpi0n wrote:If I understood your question, you mean how I checked that bitcoind is not running neither as my user or as root, right?

No. I meant how are you enabling it as a user service?

PsySc0rpi0n wrote:I might need to create this file under one of these folers

Yes, that's right.
User avatar
Head_on_a_Stick
 
Posts: 11454
Joined: 2014-06-01 17:46
Location: /dev/chair

Re: start a service only after Network is up and running

Postby PsySc0rpi0n » 2020-03-01 20:08

Head_on_a_Stick wrote:
PsySc0rpi0n wrote:If I understood your question, you mean how I checked that bitcoind is not running neither as my user or as root, right?

No. I meant how are you enabling it as a user service?


Well, as I don't know how to answer that, then I guess I'm not enabling it as such. I don't know how to do it.

Edited;
Is it the multi.user.target thing?

Head_on_a_Stick wrote:
PsySc0rpi0n wrote:I might need to create this file under one of these folers

Yes, that's right.


Ok, I'm going to move the file into
Code: Select all
~/.config/systemd/user/*


Edited;
I just noticed I don't have this folder. I think I'm supposed to create it!
User avatar
PsySc0rpi0n
 
Posts: 181
Joined: 2012-10-24 13:54
Location: Portugal

Re: start a service only after Network is up and running

Postby Head_on_a_Stick » 2020-03-01 20:16

PsySc0rpi0n wrote:I guess I'm not enabling it as such. I don't know how to do it

Read the systemctl(1) man page, it's very good (if a bit long).

The --user option is for user services.

And that [Install] section won't work, multi-user.target might do the trick. Order your services after network-online.target in the [Unit] section. Consult the systemd.unit(5) man page for the details (Requires=, Requisite=, After=, BindsTo=, Wants=, etc).
User avatar
Head_on_a_Stick
 
Posts: 11454
Joined: 2014-06-01 17:46
Location: /dev/chair

Re: start a service only after Network is up and running

Postby PsySc0rpi0n » 2020-03-01 20:58

Head_on_a_Stick wrote:
PsySc0rpi0n wrote:I guess I'm not enabling it as such. I don't know how to do it

Read the systemctl(1) man page, it's very good (if a bit long).

The --user option is for user services.

And that [Install] section won't work, multi-user.target might do the trick. Order your services after network-online.target in the [Unit] section. Consult the systemd.unit(5) man page for the details (Requires=, Requisite=, After=, BindsTo=, Wants=, etc).


You mean to start the service like:
Code: Select all
systemctl --user start bitcoind


or

Code: Select all
systemctl --user start bitcoind.service

??

Well, I have read parts of systemd.unit(5). I came across those Requires, Requisite, Wants, etc, but I'm not sure I understand correctly what is said there.

What you mean the [Install] section don't work but the multi-user.target might do the trick? If that section won't work how the multi-user.target will do the trick?

As of now, I created the folders ~/.config/systemd and ~/.config/systemd/user and inside the latter, I have created bitcoind.service. The contents are now like this:

Code: Select all
[Unit]
Description=bitcoind
After=network-online.target
Wants=network-online.target
[service]
Type=simple
User=narayan
Restart=Always
RestartSec=5
StartLimitBurst=10
StartLimitIntervalSec=50
ExecStart=/usr/bin/screen -S bitcoind -fa -D -m /usr/local/bin/bitcoind
[Install]
WantedBy=multi-user.target
User avatar
PsySc0rpi0n
 
Posts: 181
Joined: 2012-10-24 13:54
Location: Portugal

Re: start a service only after Network is up and running

Postby PsySc0rpi0n » 2020-03-01 21:14

Well, after creating the folders and the file
Code: Select all
bitcoind.service


inside

Code: Select all
~/.config/systemd/user


and making sure that NetworkManager-wait-online.service is enabled, I tried to start the service I just created but it didn't work. I tried these commands, all inside the folder where bitcoid.service is:

Code: Select all
systemctl --user start bitcoind


I got:
Failed to start bitcoind.service: Unit bitcoind.service not found.

_______________
I tried:
Code: Select all
systemctl --user start bitcoind.service


I got:
Failed to start bitcoind.service: Unit bitcoind.service not found.

_______________
I tried:
Code: Select all
sudo systemctl --user start bitcoind


I got:
Failed to connect to bus: No such file or directory

_______________
I tried:
Code: Select all
sudo systemctl --user start bitcoind.service


I got:
Failed to connect to bus: No such file or directory


So, what am I missing now?
User avatar
PsySc0rpi0n
 
Posts: 181
Joined: 2012-10-24 13:54
Location: Portugal

Re: start a service only after Network is up and running

Postby PsySc0rpi0n » 2020-03-01 21:51

Ok, I manage to find the initial problem but it's still not working due to several other errors now related with permissions, I guess.

I typed [service] instead of [Service].

[Unit]
Description=bitcoind
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
User=narayan
Restart=Always
RestartSec=5
StartLimitBurst=10
StartLimitIntervalSec=50
ExecStart=/usr/bin/screen -S bitcoind -fa -D -m /usr/local/bin/bitcoind

[Install]
WantedBy=multi-user.target

Now I get these errors in /var/log/sys.log
Mar 1 22:29:20 narayan systemd[1337]: /etc/systemd/user/bitcoind.service:8: Failed to parse service restart specifier, ignoring: Always
Mar 1 22:29:20 narayan systemd[1337]: /etc/systemd/user/bitcoind.service:11: Unknown lvalue 'StartLimitIntervalSec' in section 'Service', ignoring
Mar 1 22:29:20 narayan systemd[1337]: Started bitcoind.
Mar 1 22:29:20 narayan systemd[8625]: bitcoind.service: Failed to determine supplementary groups: Operation not permitted
Mar 1 22:29:20 narayan systemd[8625]: bitcoind.service: Failed at step GROUP spawning /usr/bin/screen: Operation not permitted
Mar 1 22:29:20 narayan systemd[1337]: bitcoind.service: Main process exited, code=exited, status=216/GROUP
Mar 1 22:29:20 narayan systemd[1337]: bitcoind.service: Failed with result 'exit-code'.



Eited;
ok, I fixed another typo about Always. It should be always...

But I still get errors in /vat/log/syslog.
Mar 1 22:48:51 narayan systemd[1337]: Started bitcoind.
Mar 1 22:48:51 narayan systemd[9280]: bitcoind.service: Failed to determine supplementary groups: Operation not permitted
Mar 1 22:48:51 narayan systemd[9280]: bitcoind.service: Failed at step GROUP spawning /usr/bin/screen: Operation not permitted
Mar 1 22:48:51 narayan systemd[1337]: bitcoind.service: Main process exited, code=exited, status=216/GROUP
Mar 1 22:48:51 narayan systemd[1337]: bitcoind.service: Failed with result 'exit-code'.


And now, when I stopped the service, I saw in /var/log/syslog thee following:
Mar 1 22:49:33 narayan systemd[1337]: /etc/systemd/user/bitcoind.service:11: Unknown lvalue 'StartLimitIntervalSec' in section 'Service', ignoring
Mar 1 22:49:33 narayan systemd[1337]: /etc/systemd/user/bitcoind.service:11: Unknown lvalue 'StartLimitIntervalSec' in section 'Service', ignoring


But I can see this setting in man systemd.unit 5.
Why it says it is not recognized?
User avatar
PsySc0rpi0n
 
Posts: 181
Joined: 2012-10-24 13:54
Location: Portugal

Re: start a service only after Network is up and running

Postby Head_on_a_Stick » 2020-03-02 17:57

Does this bitcoind thing need root permissions? I'm not familiar with it but I was presuming you wanted to launch the screen programs as your normal user, hence my advice to employ a user service.

Anyway, use this for inspiration: https://bbs.archlinux.org/viewtopic.php?id=129509 ← that shows how to launch programs on a specific TTY (which you will need to do if you want to actually view them).

Try to keep the unit files as simple as possible until you get them working. You can always add other options later.
User avatar
Head_on_a_Stick
 
Posts: 11454
Joined: 2014-06-01 17:46
Location: /dev/chair

Re: start a service only after Network is up and running

Postby PsySc0rpi0n » 2020-03-02 19:44

Head_on_a_Stick wrote:Does this bitcoind thing need root permissions? I'm not familiar with it but I was presuming you wanted to launch the screen programs as your normal user, hence my advice to employ a user service.

Anyway, use this for inspiration: https://bbs.archlinux.org/viewtopic.php?id=129509 ← that shows how to launch programs on a specific TTY (which you will need to do if you want to actually view them).

Try to keep the unit files as simple as possible until you get them working. You can always add other options later.


Thank you for that link. I added it to bookmarks.

Anyways, I manage to get it working yet yesterday with the help of some other guy in old IRC. It's always a very goo source of info, knowledge and help.

Here is the file:
Code: Select all
[Unit]
Description=bitcoind
StartLimitIntervalSec=50
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
Restart=always
RestartSec=5
StartLimitBurst=10
ExecStart=/usr/bin/screen -S bitcoind -fa -D -m /usr/local/bin/bitcoind

[Install]
WantedBy=multi-user.target


And it is running as my user when I launch htop. So, I guess it's ok now. I only need one last testing which is if it is starting on boot and after the Network is up and running. But I'll check that next time I need to reboot!

Last but not the least, I also want to thank you for your help!
Psy
User avatar
PsySc0rpi0n
 
Posts: 181
Joined: 2012-10-24 13:54
Location: Portugal

Next

Return to System configuration

Who is online

Users browsing this forum: No registered users and 14 guests

fashionable