Contenu | Rechercher | Menus

Annonce

Si vous avez des soucis pour rester connecté, déconnectez-vous puis reconnectez-vous depuis ce lien en cochant la case
Me connecter automatiquement lors de mes prochaines visites.

À propos de l'équipe du forum.

#1 Le 04/04/2017, à 13:34

Indochineom

[Résolu] Problème création d'un "Daemon" sur "Ubuntu Server 16.04 LTS"

Salutation,

j'essaie depuis plus d'une semaine de créer un daemon pour cela j'ai suivis ce tuto -> comment_transformer_un_programme_en_service

malheureusement, je n'y arrive pas j'ai énormément d'erreur je dois louper quelque chose, donc pour voir tout cela avec vous je vais vous détaillés ce que je veux faire et pourquoi, puis les erreurs rencontré  :

Tout d'abord  je veux créer un daemon pour pouvoir exécuter/stoper (voir update) un serveur de jeu plus simplement qu'en écrivant dans la console toutes les lignes de commandes que j'ai besoin.

Ce qui devrait me permettre par la suite de faire une petit page web, qui me servira à exécuter tout cela sans console.

Mon Daemon
Nom du script : ServerFFA.sh
Chemin absolu : /etc/init.d/

#!/bin/sh -e
 
DAEMON="/home/steam/server/squad/Config/StartServerFFA.sh" #ligne de commande du programme, attention à l'extension .py.
daemon_OPT=""  #argument à utiliser par le programme
DAEMONUSER="steam" #utilisateur du programme
daemon_NAME="ServerFFA" #Nom du programme (doit être identique à l'exécutable).
#Attention le script est un script bash, le script ne portera donc pas l'extension .py mais .sh.
 
PATH="/sbin:/bin:/usr/sbin:/usr/bin" #Ne pas toucher
 
test -x $DAEMON || exit 0
 
. /lib/lsb/init-functions
 
d_start () {
        log_daemon_msg "Starting system $daemon_NAME Daemon"
	start-stop-daemon --background --name $daemon_NAME --start --quiet --chuid $DAEMONUSER --exec $DAEMON -- $daemon_OPT
        log_end_msg $?
}
 
d_stop () {
        log_daemon_msg "Stopping system $daemon_NAME Daemon"
        start-stop-daemon --name $daemon_NAME --stop --retry 5 --quiet --name $daemon_NAME
	log_end_msg $?
}
 
case "$1" in
 
        start|stop)
                d_${1}
                ;;
 
        restart|reload|force-reload)
                        d_stop
                        d_start
                ;;
 
        force-stop)
               d_stop
                killall -q $daemon_NAME || true
                sleep 2
                killall -q -9 $daemon_NAME || true
                ;;
 
        status)
                status_of_proc "$daemon_NAME" "$DAEMON" "system-wide $daemon_NAME" && exit 0 || exit $?
                ;;
        *)
                echo "Usage: /etc/init.d/$daemon_NAME {start|stop|force-stop|restart|reload|force-reload|status}"
                exit 1
                ;;
esac
exit 0

Mon Script de Lancement
Nom du script : StartServerFFA.sh
Chemin absolu : /home/steam/server/squad/Config/

#!/bin/bash 
screen -d -m -S SQUAD_FFA su steam -c "/home/steam/server/squad/SquadServer.sh MULTIHOME=178.32.107.243 Port=7787 QueryPort=27025 SERVERCONFIGSDIR=/home/steam/server/squad/Config/SQUAD_FFA FIXEDMAXPLAYERS=80 RANDOM=NONE -log"

Voici mon erreur :

root@localhost:~# sudo /etc/init.d/ServerFFA start
/bin/sh: 0: Illegal option -

puis en cherchant sur l'internet j'ai trouvé ceci aussi donc je test :

root@localhost:/etc/init.d# sudo update-rc.d ServerFFA enable
update-rc.d: error: ServerFFA Default-Start contains no runlevels, aborting.

Voilà, je me tien à vôtre disposition s'il manque des renseignements.
Merci d'avance pour vôtre aide.

Cordialement,
Indo

Dernière modification par Indochineom (Le 12/04/2017, à 07:34)

Hors ligne

#2 Le 04/04/2017, à 14:18

J5012

Re : [Résolu] Problème création d'un "Daemon" sur "Ubuntu Server 16.04 LTS"

le tuto est obsolete ... il manque qqs informations d'entete au script, rendus obligatoires pour formaliser les scripts init.d → http://manpages.ubuntu.com/manpages/xen … c.d.8.html

...
       /etc/init.d/skeleton
              Model for use by writers of init.d scripts.
...

cat /etc/init.d/skeleton

#!/bin/sh
# kFreeBSD do not accept scripts as interpreters, using #!/bin/sh and sourcing.
if [ true != "$INIT_D_SCRIPT_SOURCED" ] ; then
    set "$0" "$@"; INIT_D_SCRIPT_SOURCED=true . /lib/init/init-d-script
fi
### BEGIN INIT INFO
# Provides:          skeleton
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Example initscript
# Description:       This file should be used to construct scripts to be
#                    placed in /etc/init.d.  This example start a
#                    single forking daemon capable of writing a pid
#                    file.  To get other behavoirs, implemend
#                    do_start(), do_stop() or other functions to
#                    override the defaults in /lib/init/init-d-script.
### END INIT INFO

# Author: Foo Bar <foobar@baz.org>
#
# Please remove the "Author" lines above and replace them
# with your own name if you copy and modify this script.

DESC="Description of the service"
DAEMON=/usr/sbin/daemonexecutablename

Hors ligne

#3 Le 04/04/2017, à 14:44

Indochineom

Re : [Résolu] Problème création d'un "Daemon" sur "Ubuntu Server 16.04 LTS"

D'accord effectivement ca va mieux enfin presque j'ai cette erreur maintenant :

root@localhost:/etc/init.d# ServerFFA start
-bash: /usr/sbin/ServerFFA: /bin/sh^M: bad interpreter: No such file or directory

Voici le script modifié :

cat /etc/init.d/skeleton

#!/bin/sh
# kFreeBSD do not accept scripts as interpreters, using #!/bin/sh and sourcing.
if [ true != "$INIT_D_SCRIPT_SOURCED" ] ; then
    set "$0" "$@"; INIT_D_SCRIPT_SOURCED=true . /lib/init/init-d-script
fi
### BEGIN INIT INFO
# Provides:          skeleton
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Example initscript
# Description:       This file should be used to construct scripts to be
#                    placed in /etc/init.d.  This example start a
#                    single forking daemon capable of writing a pid
#                    file.  To get other behavoirs, implemend
#                    do_start(), do_stop() or other functions to
#                    override the defaults in /lib/init/init-d-script.
### END INIT INFO

# Author: Foo Bar <foobar@baz.org>
#
# Please remove the "Author" lines above and replace them
# with your own name if you copy and modify this script.

DESC="Service de Gestion du Server FFA"
DAEMON=/usr/sbin/ServerFFA
DAEMON="/home/steam/server/squad/Config/StartServerFFA.sh" #ligne de commande du programme, attention à l'extension .py.
daemon_OPT=""  #argument à utiliser par le programme
DAEMONUSER="steam" #utilisateur du programme
daemon_NAME="ServerFFA" #Nom du programme (doit être identique à l'exécutable).
#Attention le script est un script bash, le script ne portera donc pas l'extension .py mais .sh.
 
PATH="/sbin:/bin:/usr/sbin:/usr/bin" #Ne pas toucher
 
test -x $DAEMON || exit 0
 
. /lib/lsb/init-functions
 
d_start () {
        log_daemon_msg "Starting system $daemon_NAME Daemon"
	start-stop-daemon --background --name $daemon_NAME --start --quiet --chuid $DAEMONUSER --exec $DAEMON -- $daemon_OPT
        log_end_msg $?
}
 
d_stop () {
        log_daemon_msg "Stopping system $daemon_NAME Daemon"
        start-stop-daemon --name $daemon_NAME --stop --retry 5 --quiet --name $daemon_NAME
	log_end_msg $?
}
 
case "$1" in
 
        start|stop)
                d_${1}
                ;;
 
        restart|reload|force-reload)
                        d_stop
                        d_start
                ;;
 
        force-stop)
               d_stop
                killall -q $daemon_NAME || true
                sleep 2
                killall -q -9 $daemon_NAME || true
                ;;
 
        status)
                status_of_proc "$daemon_NAME" "$DAEMON" "system-wide $daemon_NAME" && exit 0 || exit $?
                ;;
        *)
                echo "Usage: /etc/init.d/$daemon_NAME {start|stop|force-stop|restart|reload|force-reload|status}"
                exit 1
                ;;
esac
exit 0

Dernière modification par Indochineom (Le 06/04/2017, à 13:53)

Hors ligne

#4 Le 07/04/2017, à 01:22

J5012

Re : [Résolu] Problème création d'un "Daemon" sur "Ubuntu Server 16.04 LTS"

tu avais compris que le fichier skeleton n'etait pas à modifier tel quel ?

le fichier sert de base modele pour ecrire tes propres script init ...

Hors ligne

#5 Le 07/04/2017, à 11:44

Indochineom

Re : [Résolu] Problème création d'un "Daemon" sur "Ubuntu Server 16.04 LTS"

D'accord,
Si j'ai bien compris mon fichier "ServerFFA" doit être une copie du fichier skeleton dans le répertoire /etc/init.d/
ensuite je dois mettre mon script perso dans un fichier du nom de "ServerFFA" dans /usr/sbin/
Puis exécuter la commande ServerFFA start, c'est bien ca ?

Hors ligne

#6 Le 07/04/2017, à 12:35

jplemoine

Re : [Résolu] Problème création d'un "Daemon" sur "Ubuntu Server 16.04 LTS"

Indochineom a écrit :
-bash: /usr/sbin/ServerFFA: /bin/sh^M: bad interpreter: No such file or directory

Erreur typique du modification vers "dans le DOS" et envoyé via FTP avec un mauvais paramètre.
La ligne doit être

#! /bin/sh

et non

#! /bin/sh^M

Le retour à la ligne type windows est 0D 0A (13 10)
Celui coté linux est 0A (0)
Le code 0D (13) (qui correspond au CTRL-M noté ^M) est donc surnuméraire.


Membre de l'ALDIL (Association Lyonnaise pour le Développement de l'Informatique Libre)
- En pro, après 20 ans de développement, administrateur Linux / Unix depuis Avril 2019.
- En privé, sous Ubuntu-Xubuntu depuis 2009.

Hors ligne

#7 Le 07/04/2017, à 15:41

Indochineom

Re : [Résolu] Problème création d'un "Daemon" sur "Ubuntu Server 16.04 LTS"

Très intéressant, merci de ton aide jplemoine je ferais tout par nano maintenant xD

Hors ligne

#8 Le 08/04/2017, à 09:34

J5012

Re : [Résolu] Problème création d'un "Daemon" sur "Ubuntu Server 16.04 LTS"

Indochineom a écrit :

D'accord,
Si j'ai bien compris mon fichier "ServerFFA" doit être une copie du fichier skeleton dans le répertoire /etc/init.d/
ensuite je dois mettre mon script perso dans un fichier du nom de "ServerFFA" dans /usr/sbin/
Puis exécuter la commande ServerFFA start, c'est bien ca ?

tu dois distinguer le script exécutable ServerFFA
du script de service qui "demonise" ServerFFA

le script executable ServerFFA : /home/steam/server/squad/Config/StartServerFFA.sh
le script de service : /etc/init.d/serviceFFA

exemple de service : apache2
→ script de service : /etc/init.d/apache2 → ci-joint un extrait du service :

#!/bin/sh
### BEGIN INIT INFO
# Provides:          apache2
# Required-Start:    $local_fs $remote_fs $network $syslog $named
# Required-Stop:     $local_fs $remote_fs $network $syslog $named
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# X-Interactive:     true
# Short-Description: Apache2 web server
# Description:       Start the web server
#  This script will start the apache2 web server.
### END INIT INFO

DESC="Apache httpd web server"
NAME=apache2
DAEMON=/usr/sbin/$NAME
...

→ le binaire du demon : /usr/sbin/apache2

dans ton exemple à toi du server FFA :
→ tu dois mettre le script dans un chemin accessible par le systeme, le /home est deconseillé pour les services (home n'est pas encore actif au moment où init declenche les services), ca doit etre normalement /srv avec les bons droits et permissions ... quelque chose comme /srv/steam/srvffa

#!/bin/sh
### BEGIN INIT INFO
# Provides:          srvffa
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Server FFA
# Description:       Start the FFA Server
### END INIT INFO

test -x $DAEMON || exit 0

. /lib/lsb/init-functions

PATH=/sbin:/usr/sbin:/bin:/usr/bin

DESC="FFA Server"
NAME=srvffa
DAEMON=/srv/steam/$NAME
daemon_OPT=""  #argument à utiliser par le programme
DAEMONUSER="steam" #utilisateur du programme

#
# Function that starts the daemon/service , var funct are predefined in /lib/lsb/init-functions
#

d_start () {
        log_daemon_msg "Starting system $NAME Daemon"
	start-stop-daemon --background --name $NAME --start --quiet --chuid $DAEMONUSER --exec $DAEMON -- $daemon_OPT
        log_end_msg $?
}
 
d_stop () {
        log_daemon_msg "Stopping system $NAME Daemon"
        start-stop-daemon --name $NAME --stop --retry 5 --quiet --name $NAME
	log_end_msg $?
}
 
case "$1" in 
        start)
                d_start
                ;;
        stop)
                d_stop
                ;;
        *)
                echo "Usage: /etc/init.d/$NAME {start|stop}"
                exit 1
                ;;
esac
exit 0

Hors ligne

#9 Le 08/04/2017, à 11:45

Indochineom

Re : [Résolu] Problème création d'un "Daemon" sur "Ubuntu Server 16.04 LTS"

Salut J5012,

merci pour ton aide, j'ai suivis tes instructions à la lettre je pense qu'on est pas loin du but mais le script de se lance pas, je récapitule pour être sur que j'ai bien compris :

root@localhost:/etc/init.d# ServerFFA start
 * Starting system srvffa Daemon                                                    [ OK ]
root@localhost:/etc/init.d# ServerFFA stop
 * Stopping system srvffa Daemon                                                    [fail]
root@localhost:/etc/init.d#

Quand je regarde si mon serveur est start, il ne l'est pas.

J'ai mis mon exécutable dans /srv/steam/srvffa :

#!/bin/sh
su steam -c "/home/steam/server/squad/SquadServer.sh MULTIHOME=178.32.107.243 Port=7777 QueryPort=27015 SERVERCONFIGSDIR=/home/steam/server/squad/Config/SQUAD_FFA FIXEDMAXPLAYERS=80 RANDOM=NONE -log"

J'ai créer un fichier ServerFFA avec ceci dedans, il est dans /usr/sbin

#!/bin/sh
### BEGIN INIT INFO
# Provides:          srvffa
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Server FFA
# Description:       Start the FFA Server
### END INIT INFO

test -x $DAEMON || exit 0

. /lib/lsb/init-functions

PATH=/sbin:/usr/sbin:/bin:/usr/bin

DESC="FFA Server"
NAME=srvffa
DAEMON=/srv/steam/$NAME
daemon_OPT=""  #argument à utiliser par le programme
DAEMONUSER="steam" #utilisateur du programme

#
# Function that starts the daemon/service , var funct are predefined in /lib/lsb/init-functions
#

d_start () {
        log_daemon_msg "Starting system $NAME Daemon"
        start-stop-daemon --background --name $NAME --start --quiet --chuid $DAEMONUSER --exec $DAE$
        log_end_msg $?
}

d_stop () {
        log_daemon_msg "Stopping system $NAME Daemon"
        start-stop-daemon --name $NAME --stop --retry 5 --quiet --name $NAME
        log_end_msg $?
}

case "$1" in
        start)
                d_start
                ;;
        stop)
                d_stop
                ;;
        *)
                echo "Usage: /etc/init.d/$NAME {start|stop}"
                exit 1
                ;;
esac
exit 0

Puis toujours le fichier ServerFFA dans /etc/init.d :

#!/bin/sh
# kFreeBSD do not accept scripts as interpreters, using #!/bin/sh and sourcing.
if [ true != "$INIT_D_SCRIPT_SOURCED" ] ; then
    set "$0" "$@"; INIT_D_SCRIPT_SOURCED=true . /lib/init/init-d-script
fi
### BEGIN INIT INFO
# Provides:          skeleton
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Example initscript
# Description:       This file should be used to construct scripts to be
#                    placed in /etc/init.d.  This example start a
#                    single forking daemon capable of writing a pid
#                    file.  To get other behavoirs, implemend
#                    do_start(), do_stop() or other functions to
#                    override the defaults in /lib/init/init-d-script.
### END INIT INFO

# Author: Foo Bar <foobar@baz.org>
#
# Please remove the "Author" lines above and replace them
# with your own name if you copy and modify this script.

DESC="Service serveur squad ffa"
DAEMON=/usr/sbin/ServerFFA

Encore merci pour ton aide !

Dernière modification par Indochineom (Le 08/04/2017, à 11:45)

Hors ligne

#10 Le 12/04/2017, à 07:33

Indochineom

Re : [Résolu] Problème création d'un "Daemon" sur "Ubuntu Server 16.04 LTS"

Salut ,
problème réglé avec systemd et c'est vraiment mieux !

Merci wink

Hors ligne