Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run as system daemon #93

Closed
nidico opened this issue Jan 23, 2017 · 17 comments
Closed

Run as system daemon #93

nidico opened this issue Jan 23, 2017 · 17 comments

Comments

@nidico
Copy link

nidico commented Jan 23, 2017

What's the best practice in order to run openfortivpn as a system daemon, e.g. as a systemd unit? I'm particularly concerned about automatic reconnection in case of connection loss.

@adrienverge
Copy link
Owner

Hi @nidico,

You need to create a systemd config file (either in /etc/systemd/system or /usr/lib/systemd/system), for example openfortivpn.service. I'm not a systemd expert but I guess you would need:

[Install]
After=NetworkManager.service

Once done:

sudo systemctl daemon-reload
sudo systemctl start openfortivpn
sudo systemctl enable openfortivpn

@nidico
Copy link
Author

nidico commented Feb 22, 2017

For others looking at this: This isn't working (i.e. automatically restarting) for me as it is (though a similar systemd unit in general is). I haven't spend more time since and setup a cron job which restarts the systemd service if the connection is down.

@r3tr0g4m3r
Copy link

r3tr0g4m3r commented Mar 12, 2018

This is my service running at boot in CentOS 7.4:
/usr/lib/systemd/system/openfortivpn.service

Description = OpenFortiVPN
After=network-online.target multi-user.target
Documentation=man:openfortivpn(1)

[Service]
User=root
Type=idle
ExecStart = /usr/local/bin/openfortivpn -c /etc/openfortivpn/config
KillSignal=SIGTERM

[Install]
WantedBy=multi-user.target

@deevictor
Copy link

/etc/openfortivpn/config

Type=forking , otherwise systemd will show active status even if VPN connection is not established.

@DimitriPapadopoulos
Copy link
Collaborator

DimitriPapadopoulos commented Jul 24, 2021

Type=forking doesn't look right here, because openfortivpn does not fork. Doesn't Type=simple work for you?

@deevictor
Copy link

my bad, the service is not start with forking or simple type, only with Type=idle as mentioned r3tr0g4m3r.

@DimitriPapadopoulos
Copy link
Collaborator

DimitriPapadopoulos commented Jul 26, 2021

Type notify might be useful too as openfortivpn calls sd_notify() to notify systemd when the tunnel is up.

@iKlsR
Copy link

iKlsR commented Oct 5, 2021

@DimitriPapadopoulos I was setting up this and when I switch to notify it gets stuck in this state until it fails, going back to simple works.

ploi@data-server:~$ /usr/bin/openfortivpn --version
1.6.0
ploi@data-server:~/data.redacted.com/itch$ systemctl status openfortivpn.service
● openfortivpn.service - Forti VPN Api Service
   Loaded: loaded (/etc/systemd/system/openfortivpn.service; enabled; vendor preset: enabled)
   Active: activating (start) since Tue 2021-10-05 13:07:02 UTC; 19s ago
     Docs: man:openfortivpn(1)
 Main PID: 19945 (openfortivpn)
    Tasks: 6 (limit: 1150)
   CGroup: /system.slice/openfortivpn.service
           ├─19945 /usr/bin/openfortivpn -c /etc/openfortivpn/config
           └─19960 /usr/sbin/pppd 38400 :1.1.1.1 noipdefault noaccomp noauth default-asyncmap nopcomp receive-all nodefaultroute nodetach lcp-max-config
[Unit]
Description=OpenFortiVPN Service
After=network-online.target multi-user.target
Documentation=man:openfortivpn(1)

[Service]
User=root
Type=simple
# WorkingDirectory=/root
ExecStart=/usr/bin/openfortivpn -c /etc/openfortivpn/config
Restart=always
RestartSec=5
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=openfortivpn
KillSignal=SIGTERM

[Install]
WantedBy=multi-user.target

@DimitriPapadopoulos
Copy link
Collaborator

it gets stuck in this state until it fails

In which state? How does it fail?

@iKlsR
Copy link

iKlsR commented Oct 5, 2021

With notify when I reload and restart I get

● openfortivpn.service - OpenFortiVPN Service
   Loaded: loaded (/etc/systemd/system/openfortivpn.service; enabled; vendor preset: enabled)
   Active: activating (start) since Tue 2021-10-05 13:05:26 UTC; 1s ago
     Docs: man:openfortivpn(1)
 Main PID: 19719 (openfortivpn)
    Tasks: 7 (limit: 1150)
   CGroup: /system.slice/openfortivpn.service
           ├─19719 /usr/bin/openfortivpn -c /etc/openfortivpn/config
           └─19734 /usr/sbin/pppd 38400 :1.1.1.1 noipdefault noaccomp noauth default-asyncmap nopcomp receive-all nodefaultroute nodetach lcp-max-config

as opposed to using simple

● openfortivpn.service - OpenFortiVPN Service
   Loaded: loaded (/etc/systemd/system/openfortivpn.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2021-10-05 13:14:15 UTC; 25min ago
     Docs: man:openfortivpn(1)
 Main PID: 20310 (openfortivpn)
    Tasks: 6 (limit: 1150)
   CGroup: /system.slice/openfortivpn.service
           ├─20310 /usr/bin/openfortivpn -c /etc/openfortivpn/config
           └─20325 /usr/sbin/pppd 38400 :1.1.1.1 noipdefault noaccomp noauth default-asyncmap nopcomp receive-all nodefaultroute nodetach lcp-max-config

I see nothing useful in syslog or with journalctl, if I do hostname -I I can see the expected ip while it's in that state but I can't connect. I can't try to reproduce this now as I need the vpn running to capture some itch data but I will try again with notify tomorrow as I believe there was something in the logs yesterday when I was playing with this.

ploi@data-server:~$ sudo systemctl start openfortivpn.service
Job for openfortivpn.service failed because a timeout was exceeded.

@DimitriPapadopoulos
Copy link
Collaborator

DimitriPapadopoulos commented Oct 20, 2021

ploi@data-server:~$ /usr/bin/openfortivpn --version
1.6.0

Version 1.6.0 is pretty old and does not call sd_notify(). My recommendation would be to:

  • use exec with openfortivpn binaries that do not call sd_notify() (versions prior to 1.8.0),
  • use notify with openfortivpn binaries that do call sd_notify() (versions 1.8.0 and newer built with systemd support).

@DimitriPapadopoulos
Copy link
Collaborator

Also see #948.

@requa3r0
Copy link

The standard lease time for fortify vpn server side is 8 hours.

Has as anyone figured out how to automatically reconnect if the connection is lost or the lease time expires.

@mrbaseman
Copy link
Collaborator

@requa3r0 see the above discussion about how to start openfortivpn as a daemon. If your login needs a one time password or any other 2nd factor, it is simply not possible to automate the reconnect

@kardoka
Copy link

kardoka commented Feb 2, 2024

@mrbaseman If the connection is based on a username + password combination, is it possible to implement auto-reconnect?

@requa3r0
Copy link

requa3r0 commented Feb 4, 2024

@kardoka Really

this daemon reconnects for days...1 sec after the lease time is over.

@requa3r0
Copy link

requa3r0 commented Feb 4, 2024

@kardoka @mrbaseman
Here:

[Unit]
Description=openfortivpn-daemon
After=network-online.target
Wants=network-online.target systemd-networkd-wait-online.service

StartLimitIntervalSec=500
StartLimitBurst=5

[Service]
Restart=on-failure
RestartSec=5s

ExecStart=/usr/bin/openfortivpn

[Install]
WantedBy=multi-user.target

DimitriPapadopoulos added a commit to DimitriPapadopoulos/openfortivpn that referenced this issue Feb 4, 2024
DimitriPapadopoulos added a commit that referenced this issue Feb 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants