3

I'm trying to setup a bash script as a network service listening on tcp port 6666. But service fail at startup with this errors :

 heartbeat]# systemctl status heartbeat.service
● heartbeat.service - Service de collecte des signaux de vie
   Loaded: loaded (/etc/systemd/system/heartbeat.service; disabled; vendor preset: disabled)
   Active: failed (Result: resources)

mai 12 12:32:48 Chalet systemd[1]: heartbeat.service: Got more than one socket.
mai 12 12:32:48 Chalet systemd[1]: heartbeat.service: Failed to run 'start' task: Invalid argument
mai 12 12:32:48 Chalet systemd[1]: Failed to start Service de collecte des signaux de vie.
mai 12 12:32:48 Chalet systemd[1]: heartbeat.service: Unit entered failed state.
mai 12 12:32:48 Chalet systemd[1]: heartbeat.service: Failed with result 'resources'.
mai 12 12:33:18 Chalet systemd[1]: heartbeat.service: Got more than one socket.
mai 12 12:33:18 Chalet systemd[1]: heartbeat.service: Failed to run 'start' task: Invalid argument
mai 12 12:33:18 Chalet systemd[1]: Failed to start Service de collecte des signaux de vie.
mai 12 12:33:18 Chalet systemd[1]: heartbeat.service: Failed with result 'resources'.

I don't understand wath this message "heartbeat.service: Got more than one socket" mean. Can u help me understand where i'm wrong ?

I configured systemd like this :

heartbeat.socket :

[Unit]
Description=Socket pour le demon heartbeat
PartOf=heartbeat.service

[Socket]
ListenStream=0.0.0.0:6666
Accept=true

[Install]
WantedBy=sockets.target

heartbeat.service :

[Unit]
Description=Service de collecte des signaux de vie
After=network.target heartbeat.socket
Requires=heartbeat.socket

[Service]
Type=simple
ExecStart=/bin/bash /usr/heartbeat/heartbeat.bash
RemainAfterExit=no
StandardInput=socket
StandardOutput=inherit

[Install]
WantedBy=multi-user.target

/usr/heartbeat/heartbeat.bash is :

 #!/bin/bash
while true
do
        read -r entree
        if [[ $entree == "frequence" ]]
        then
                echo "3600"
        fi
        if [[ -n $entree ]]
        then
                logger "heartbeat receveid : $entree"
        fi
done
3

Renaming heartbeat.service heartbeat@.service did the job.

The answer was simply in the systemd.socket manpage (RTFM twice if one is not enough...) :

For each socket file, a matching service file must exist, describing the service to start on incoming traffic on the socket (see systemd.service(5) for more information about .service files). The name of the .service unit is by default the same as the name of the .socket unit, but can be altered with the Service= option described below. Depending on the setting of the Accept= option described below, this .service unit must either be named like the .socket unit, but with the suffix replaced, unless overridden with Service=; or it must be a template unit named the same way. Example: a socket file foo.socket needs a matching service foo.service if Accept=false is set. If Accept=true is set, a service template file foo@.service must exist from which services are instantiated for each incoming connection.

Consequantly the service has not to be started since he is instanciated by systemd and triggered by the socket unit (wich has to be started to listen the tcp port for incoming connections)

Since we don't give any name to the instance, for those wich would be curious about the name of the instance given by %I in the service, it seems to be the instance number minus one (ie first instance has the 0 ID ):

[root@Chalet ~]# systemctl status heartbeat.socket
● heartbeat.socket - Socket pour le demon heartbeat
   Loaded: loaded (/etc/systemd/system/heartbeat.socket; disabled; vendor preset: disabled)
   Active: active (listening) since Mon 2017-05-15 15:25:03 CEST; 7h ago
   Listen: 0.0.0.0:6666 (Stream)
 Accepted: 5; Connected: 5

mai 15 15:25:03 Chalet systemd[1]: Listening on Socket pour le demon heartbeat.
[root@Chalet ~]# ncat 127.0.0.1 6666              
what is %I in the service ?
^C
[root@Chalet ~]# systemctl status heartbeat.socket
● heartbeat.socket - Socket pour le demon heartbeat
   Loaded: loaded (/etc/systemd/system/heartbeat.socket; disabled; vendor preset: disabled)
   Active: active (listening) since Mon 2017-05-15 15:25:03 CEST; 7h ago
   Listen: 0.0.0.0:6666 (Stream)
 Accepted: 6; Connected: 6

mai 15 15:25:03 Chalet systemd[1]: Listening on Socket pour le demon heartbeat.
[root@Chalet ~]# journalctl -ra                   
-- Logs begin at Sat 2016-11-19 19:12:18 CET, end at Mon 2017-05-15 23:02:21 CEST. --
mai 15 23:02:21 Chalet root[7224]: heartbeat receveid : what is %I in the service ?
mai 15 23:01:43 Chalet systemd[1]: Started Service de collecte des signaux de vie pour 5 (127.0.0.1:50920).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.