Royce
Royce

Reputation: 1595

My daemon die directly after starting

I have to develop a program with Python3 which executes automatically programs at specific times. I must use a daemon and I can't use external libraries.

That's why I create an install program which configure the daemon (I followed this tutorial https://openclassrooms.com/courses/faire-un-demon-sous-linux).

The file gobatch is the program which executes automatically programs.

#! /usr/bin/python3
# -*- coding: utf8 -*-

import os
import sys
import time
import shutil

# Check if script is start with root rights
if os.geteuid() != 0:
    exit('You need to have root privileges to run this script. Use \'sudo ./install.py\'.')

# Title
print('----- ProcessManager install ----- \n')

time.sleep(1)

# Get path of gobatch file
gobatchPath = input("Please enter the path (absolute) where the ProcessManager gobatch program is located: ")

# Check if file exists
try:
    with open(gobatchPath):
        pass
except IOError:
    exit('Error : file does not exists.')

# Copy gobatch file into /usr/bin/
shutil.copy(gobatchPath, '/usr/bin/gobatch')

# Create and fill the automatic launch program
description = 'Deamon that allows you to run cyclicaly at a date or a specific time a program'

fileContent = '#### BEGIN INIT INFO \n' \
              '# Provides:          chillispot et freeradius dans le chroot \n' \
              '# Required-Start:    $local_fs $network \n' \
              '# Required-Stop:     $local_fs $remote_fs _\n' \
              '# Default-Start:     2 3 4 5 \n' \
              '# Default-Stop:      0 1 6 \n' \
              '# Short-Description: Wireless & LAN Access Point Controller \n' \
              '# Description:       ChilliSpot is an open source captive portal \n' \
              '#                    or wireless LAN access point controller. \n' \
              '### END INIT INFO \n\n\n' \
              'DESC="' + description + '"\n' \
              'DEAMON=/usr/bin/gobatch'

file = open('/etc/init.d/gobatch', 'w')
file.write(fileContent)
file.close()

# Give the rights
os.chmod('/etc/init.d/gobatch', 0o755)

# Save gobatch file to be active
os.system('update-rc.d gobatch defaults')

However, when I start the service with : /etc/init.d/gobatch start and displaying the status with service gobatch status I get

gobatch.service - SYSV: ChilliSpot is an open source captive portal
  Loaded: loaded (/etc/init.d/gobatch; bad; vendor preset: enabled)
  Active: inactive (dead)
     Docs: man:systemd-sysv-generator(8)

Can you tell me if my way is the right method or it exists other solutions?

And do you have any idea why my daemon die directly after starting?

Thank's for help!

UPDATE

Currently, my gobatch file is not a real program because I prefer create the deamon before.

gobatch file

#! /bin/bash

echo "it works !"

My work is to create the installer of the daemon, not the gobatch program. It's another person who must do that. That's why I'm using a minmial content.

UPDATE 2:

My new /etc/init.d/gobatch :

#!/bin/sh
#### BEGIN INIT INFO 
# Provides:          chillispot et freeradius dans le chroot 
# Required-Start:    $local_fs $network 
# Required-Stop:     $local_fs $remote_fs _
# Default-Start:     2 3 4 5 
# Default-Stop:      0 1 6 
# Short-Description: Wireless & LAN Access Point Controller 
# Description:       ChilliSpot is an open source captive portal 
#                    or wireless LAN access point controller. 
### END INIT INFO 


PATH=/bin:/usr/bin:/sbin:/usr/sbin 
DESC="Deamon that allows you to run cyclicaly at a date or a specific time a program" 
NAME=gobatch 
DEAMON=/usr/sbin/gobatch 
PIDFILE=/var/run/$NAME.pid 
SCRIPTNAME=/etc/init.d/"$NAME" 

. /lib/lsb/init-functions 

case "$1" in 
start) log_daemon_msg "Starting gobatch" 
       start_daemon -p $PIDFILE $DAEMON 
       log_end_msg $? 
       ;; 
stop) log_daemon_msg "Stopping gobatch" 
      killproc -p $PIDFILE $DAEMON 
      RETVAL=$? 
      [ $RETVAL -eq 0 ] && [ -e "$PIDFILE" ] && rm -f $PIDFILE 
      log_end_msg $RETVAL 
      ;; 
restart) log_daemon_msg "Restarting gobatch" 
         $0 stop 
         $0 start 
         ;; 
esac 
exit 0

With this code I get this error :

gobatch.service: Failed at step EXEC spawning /etc/init.d/gobatch: Exec format error

UPDATE 3:

The last error into logs file is :

janv. 08 15:33:07 ubuntu systemd[1]: Failed to start SYSV: ChilliSpot is an open source captive portal.

I hope after that, my program will works.

Upvotes: 0

Views: 314

Answers (2)

Tomáš Cerha
Tomáš Cerha

Reputation: 309

Your script echoes and exits immediately, so it is not a deamon. A daemon needs to continue running after invocation.

You need to have a program that behaves like a deamon in order to test its installation. The installation will not create a deamon out of a regular program. It will only prepare the environment for running it.

The question How do you create a daemon in Python? will give you more information how to write a proper daemon in Python.

Upvotes: 1

silenzi
silenzi

Reputation: 1

I suspect that once your gobatch prints the executions is done.

Try

#! /bin/bash
for i in $(seq 1 10)
do
    echo "it works !"
    sleep 1
done

Upvotes: 0

Related Questions