Service(daemon) doesn't start in snap

Hello.
I’m trying to start a daemon from my snap package.
In snapcraft.yaml I writing:

apps:
    test:
        ...
    testDaemon: #it will working like daemon
        command: bin/run.sh $SNAP/bin/daemon.sh --start
        stop-command: bin/run.sh $SNAP/bin/daemon.sh --stop
        stop-timeout: 5s
        daemon: simple
        plugs: ['home', 'x11', 'gsettings', 'raw-usb', 'serial-port', 'network']
        environment
            LD_LIBRARY_PATH: $SNAP/libs:$LD_LIBRARY_PATH

In scrip for daemon I have:

case "$1" in
start)
    echo "$(date) demon is starting" >> /var/log/checker.txt
    $SNAP/bin/./project -D "$HOME" -T "$HOME" -S "${1:-${RANDOM}}" &
    
    if [ -e /var/run/checked.pid ]; then
        echo $!>/var/run/checked.pid
    else
        touch /var/run/checked.pid
        echo $!>/var/run/checked.pid
    exit 1
    fi
;;

stop)
    echo "$(date) demon is stopped" >> /var/log/checker.txt
    kill 'cat /var/run/checked.pid'
    rm '/var/run/checked.pid'
;;

I guess that in the script I wrote something wrong.
Correct me, please.
Thx.

Hey

Looks like you are missing a colon after environment, it should be a map, right?

As for your script, you should not store your pid or do the whole wrapper script, just run your command directly. This is not sysv init, systemd will track your daemon correctly. And similarly you don’t need any start/stop scripts, just the command you want to run.

I correctly understand you that in a script to run the daemon should only be

$SNAP/bin/./project -D "$HOME" -T "$HOME" -S "${1:-${RANDOM}}"

and nothing else?

Now, script look like

#!/bin/bash
# chkconfig: 2345 20 80
# description: load for CORE
# Author: Ananyev Alexey
#. /etc/init.d/funktions

if [ -e PrimaryKey ]
then
    rm -f PrimaryKey
fi
if [ -e SecondaryKey ]
then
    rm -f SecondaryKey
fi
$SNAP/bin/./CORE -D "$HOME" -T "$HOME" -S "${1:-${RANDOM}}" &
exit 0

drop the & from the end of the line (and i’d also drop the exit 0, it is useless when systemd manages the process …)

1 Like

Drop the chkconfig stuff, it’s just meaningless in snapd context. Unless you really know why don’t even write the whole script and just stick that last line into command field (without & as ogra indicated)

Now, script look like

#!/bin/bash
# description: load for CORE
# Author: Ananyev Alexey


#if [ -e PrimaryKey ]
#then
#    rm -f PrimaryKey
#fi
#if [ -e SecondaryKey ]
#then
#    rm -f SecondaryKey
#fi

$SNAP/bin/./CORE -D "$HOME" -T "$HOME" -S "${1:-${RANDOM}}" &

Don’t work(((

It’s log from terminal

alexey@alexey-ProBook-650:/media/free/SNAP/BSP3_X11$ systemctl status snap.bsp095project.BSP095launcherCoreDaemon.service 
● snap.bsp095project.BSP095launcherCoreDaemon.service - Service for snap application bsp095project.BSP095launcherCoreDaemon
   Loaded: loaded (/etc/systemd/system/snap.bsp095project.BSP095launcherCoreDaemon.service; enabled; vendor preset: enabled)
   Active: inactive (dead) since Čt 2017-08-17 13:16:33 CEST; 4s ago
  Process: 1574 ExecStop=/usr/bin/snap run --command=stop bsp095project.BSP095launcherCoreDaemon (code=exited, status=0/SUCCESS)
  Process: 1557 ExecStart=/usr/bin/snap run bsp095project.BSP095launcherCoreDaemon (code=exited, status=0/SUCCESS)
 Main PID: 1557 (code=exited, status=0/SUCCESS)

srp 17 13:16:32 alexey-ProBook-650 systemd[1]: Started Service for snap application bsp095project.BSP095launcherCoreDaemon.
srp 17 13:16:32 alexey-ProBook-650 /usr/bin/snap[1557]: cmd.go:189: DEBUG: restarting into "/snap/core/current/usr/bin/snap"
srp 17 13:16:32 alexey-ProBook-650 /usr/bin/snap[1574]: cmd.go:189: DEBUG: restarting into "/snap/core/current/usr/bin/snap"
alexey@alexey-ProBook-650:/media/free/SNAP/BSP3_X11$ systemctl start snap.bsp095project.BSP095launcherCoreDaemon.service 
alexey@alexey-ProBook-650:/media/free/SNAP/BSP3_X11$ systemctl status snap.bsp095project.BSP095launcherCoreDaemon.service 
● snap.bsp095project.BSP095launcherCoreDaemon.service - Service for snap application bsp095project.BSP095launcherCoreDaemon
   Loaded: loaded (/etc/systemd/system/snap.bsp095project.BSP095launcherCoreDaemon.service; enabled; vendor preset: enabled)
   Active: inactive (dead) since Čt 2017-08-17 13:16:56 CEST; 7s ago
  Process: 1677 ExecStop=/usr/bin/snap run --command=stop bsp095project.BSP095launcherCoreDaemon (code=exited, status=0/SUCCESS)
  Process: 1660 ExecStart=/usr/bin/snap run bsp095project.BSP095launcherCoreDaemon (code=exited, status=0/SUCCESS)
 Main PID: 1660 (code=exited, status=0/SUCCESS)

srp 17 13:16:56 alexey-ProBook-650 systemd[1]: Started Service for snap application bsp095project.BSP095launcherCoreDaemon.
srp 17 13:16:56 alexey-ProBook-650 /usr/bin/snap[1660]: cmd.go:189: DEBUG: restarting into "/snap/core/current/usr/bin/snap"
srp 17 13:16:56 alexey-ProBook-650 /usr/bin/snap[1677]: cmd.go:189: DEBUG: restarting into "/snap/core/current/usr/bin/snap"

again: drop the & at the end of this line (or drop the script completely and just add the line into your “command:” in snapcraft.yaml like zyga suggested …)

you are trying to mix a hacked up sysvinit script with systemd, this wont work …

1 Like

Just to be clear, do NOT make any start/stop commands, just use command under your app entry in snapcraft.yaml.

I’m sorry, my mistake.
I did not think about & at the end command

If you say do not use start/stop commands, then how can I send service from the snap to send messages about the start?
I have 3 applications in my snap, one of them is daemon.

name: bsp095project
version: "1.0"
summary: timer
description: |
    Application for medical testing
confinement: strict
architectures: [amd64]

apps:
    BSP095launcherCoreSimulation:
        command: bin/run.sh $SNAP/bin/start_CORE_simulation.sh
        plugs: ['home', 'x11', 'opengl', 'gsettings', 'raw-usb', 'serial-port', 'network']
        environment:
            LD_LIBRARY_PATH: $SNAP/btl_libs/Simulation:$SNAP/btl_libs:$LD_LIBRARY_PATH
    BSP095launcherCoreDaemon: #it will working like daemon
        command: bin/run.sh $SNAP/bin/core_daemon_new.sh --start
        stop-command: bin/run.sh $SNAP/bin/core_daemon_new.sh --stop
        stop-timeout: 5s
        daemon: simple
        plugs: ['home', 'gsettings', 'raw-usb', 'serial-port', 'network']
        environment:
            LD_LIBRARY_PATH: $SNAP/btl_libs/Simulation:$SNAP/btl_libs:$LD_LIBRARY_PATH
    BSP095launcherGui:
        command: bin/run.sh $SNAP/bin/start_GUI.sh
        plugs: ['home', 'x11', 'opengl', 'gsettings', 'pulseaudio', 'network', 'camera']
        environment:
            LD_LIBRARY_PATH: $SNAP/btl_libs/CommProtV200:$SNAP/btl_libs:$LD_LIBRARY_PATH
    BSP095launcherMaintenance:
        command: bin/run.sh $SNAP/bin/start_maintenance_hardware.sh
        plugs: ['home', 'x11', 'opengl', 'gsettings', 'pulseaudio', 'network', 'camera']
        environment:
            LD_LIBRARY_PATH: $SNAP/btl_libs/CommProtV200:$SNAP/btl_libs:$LD_LIBRARY_PATH

What does that mean?

send service from the snap to send messages about the start

Do you want to notify other applications in your snap that some parts of it have started or stopped?

If I’m trying to select from the terminal the name of the snap, what can I run, then I see only

bsp095project.BSP095launcherCoreSimulation bsp095project.BSP095launcherGui bsp095project.BSP095launcherMaintenance

I can’t see name of daemon bsp095project.BSP095launcherCoreDaemon

I can get to the demon only:

systemctl status snap.bsp095project.BSP095launcherCoreDaemon.service

but you say not use start/stop command like

systemctl start snap.bsp095project.BSP095launcherCoreDaemon.service

So how do I run the daemon with the start command?)

On the plan, the daemon starts up, creates 2 files, and after that it must tell the bsp095project.BSP095launcherGui that it can start to.

Can anyone tell me how I can run a daemon through a command, if in snapcraft.yaml I have it written like this:

BSP095launcherCoreDaemon: #it will working like daemon
    command: bin/run.sh $SNAP/bin/core_daemon_new.sh --start
    stop-command: bin/run.sh $SNAP/bin/core_daemon_new.sh --stop
    stop-timeout: 5s
    daemon: simple
    plugs: ['home', 'gsettings', 'raw-usb', 'serial-port', 'network']
    environment:
        LD_LIBRARY_PATH: $SNAP/btl_libs/Simulation:$SNAP/btl_libs:$LD_LIBRARY_PATH

and in core_daemon_new.sh I have only

#!/bin/bash
# description: load for CORE
# Author: Ananyev Alexey

$SNAP/bin/./CORE -D "$HOME" -T "$HOME" -S "${1:-${RANDOM}}"

Thanks.

Note that $HOME will probably not work for a daemon (it will run as root), you should perhaps use “$SNAP_DATA” instead…

BSP095launcherCoreDaemon: #it will working like daemon
command: bin/run.sh $SNAP/bin/./CORE -D "$HOME" -T "$HOME" -S "${1:-${RANDOM}}"
daemon: simple
plugs: ['home', 'gsettings', 'raw-usb', 'serial-port', 'network']
environment:
    LD_LIBRARY_PATH: $SNAP/btl_libs/Simulation:$SNAP/btl_libs:$LD_LIBRARY_PATH

If I understand you correctly, should it be …

BSP095launcherCoreDaemon: #it will working like daemon
        command: bin/run.sh $SNAP/bin/./CORE -D "$SNAP_DATA" -T "$SNAP_DATA" -S "${1:-${RANDOM}}"
 #       stop-command: bin/run.sh $SNAP/bin/core_daemon_new.sh --stop
 #       stop-timeout: 5s
        daemon: simple
        plugs: ['gsettings', 'raw-usb', 'serial-port', 'network']
        environment:
            LD_LIBRARY_PATH: $SNAP/btl_libs/CommProtV200:$SNAP/btl_libs:$LD_LIBRARY_PATH

I stil get error

● snap.bsp095project.BSP095launcherCoreDaemon.service - Service for snap application bsp095project.BSP095launcherCoreDaemon
   Loaded: loaded (/etc/systemd/system/snap.bsp095project.BSP095launcherCoreDaemon.service; enabled; vendor preset: enabled)
   Active: inactive (dead) (Result: signal) since Ne 2017-08-20 15:56:12 CEST; 1s ago
  Process: 1678 ExecStart=/usr/bin/snap run bsp095project.BSP095launcherCoreDaemon (code=killed, signal=ABRT)
 Main PID: 1678 (code=killed, signal=ABRT)

srp 20 15:56:12 alexey-ProBook-650 systemd[1]: snap.bsp095project.BSP095launcherCoreDaemon.service: Unit entered failed state.
srp 20 15:56:12 alexey-ProBook-650 systemd[1]: snap.bsp095project.BSP095launcherCoreDaemon.service: Failed with result 'signal'.
srp 20 15:56:12 alexey-ProBook-650 systemd[1]: snap.bsp095project.BSP095launcherCoreDaemon.service: Service hold-off time over, scheduling restart.
srp 20 15:56:12 alexey-ProBook-650 systemd[1]: Stopped Service for snap application bsp095project.BSP095launcherCoreDaemon.
srp 20 15:56:12 alexey-ProBook-650 systemd[1]: snap.bsp095project.BSP095launcherCoreDaemon.service: Start request repeated too quickly.
srp 20 15:56:12 alexey-ProBook-650 systemd[1]: Failed to start Service for snap application bsp095project.BSP095launcherCoreDaemon.