0

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.

Royce
  • 1,557
  • 5
  • 19
  • 44
  • Have you checked the system log files? – Some programmer dude Jan 08 '18 at 10:44
  • No I don't know where I can see that. Into `/var/log/syslog`? – Royce Jan 08 '18 at 10:46
  • It seems that the problem is in the `gobatch` program itself, not in the installer. Thus it would make more sense to post the source of the program instead of the installer. – Tomáš Cerha Jan 08 '18 at 10:54
  • @TomášCerha thank's for your answer. I updated my question. – Royce Jan 08 '18 at 10:59
  • 1
    As for the "Exec format error": I guess you are missing the line #!/bin/bash ath the first line of the /etc/init.d/gobash – Tomáš Cerha Jan 08 '18 at 14:24
  • Yes I forgot it... Thank's a lot! I update my question with last error presents on logs file. – Royce Jan 08 '18 at 14:34
  • Your init script should use the functions defined in /lib/lsb/init-functions. Don't know which version you have, but the latest is documented here: http://refspecs.linuxbase.org/LSB_5.0.0/LSB-Core-generic/LSB-Core-generic/iniscrptfunc.html. I don't see a function named `log_daemon_msg` there. Maybe that's the problem? – Tomáš Cerha Jan 08 '18 at 22:27
  • That's was the problem! You are my savior! – Royce Jan 09 '18 at 12:03
  • Great! It got a little messy as you ran into other problems gradually, but I believe you can still mark my original answer as the solution because it answered the original question. – Tomáš Cerha Jan 10 '18 at 07:29

2 Answers2

1

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.

Tomáš Cerha
  • 309
  • 1
  • 7
  • I need to use a loop like `while true; do done` to know if it's working for example? – Royce Jan 08 '18 at 11:07
  • Yes, a deamon will typically run in some sort of infinite loop. – Tomáš Cerha Jan 08 '18 at 11:14
  • It's for a school project and I can't use libraries or existing code. So I do not know if using http://web.archive.org/web/20131017130434/http://www.jejik.com/articles/2007/02/a_simple_unix_linux_daemon_in_python/ is a good idea. – Royce Jan 08 '18 at 11:18
  • Sorry, I noticed the last sentence of your question just now, so I was a little confused. I didn't realize that you don't care about writing the deamon itself. The problem is that 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. – Tomáš Cerha Jan 08 '18 at 11:22
  • There is no problem. Yes exaclty, my program prepares the environment to receive a daemon. And my objective is to test if the environment is correctly configured to receive a daemon program. I thought that running a program with a loop would suffice. – Royce Jan 08 '18 at 11:28
  • Because to working, `gobatch` must use function `start` and `run`? – Royce Jan 08 '18 at 11:47
  • 1
    I feel that your init.d script (`fileContent`) is not complete. It usually contains a case statement running different commands for start/status/stop/restart. – Tomáš Cerha Jan 08 '18 at 11:59
  • I think too, It's a good thing aha. However, in the case of start / stop / restart I do not know what to put as code. – Royce Jan 08 '18 at 12:06
  • The file /etc/init.d/gobatch (not /usr/bin/gobatch) should handle the start/stop/... commands. Example: http://werxltd.com/wp/2012/01/05/simple-init-d-script-template – Tomáš Cerha Jan 08 '18 at 12:07
  • I already visited this page too. But on my tutorial https://openclassrooms.com/courses/faire-un-demon-sous-linux (in french sorry) the author never talk about thoses `case`. That's why I'm lost. – Royce Jan 08 '18 at 12:12
  • Maybe it's because his script starts `/etc/init.d/apache`. And `/etc/init.d/apache` contains methods start/stop/restart. – Royce Jan 08 '18 at 12:18
  • I can see the function do_start() in section "Quelques options" and the case statement in "Sous d'autres distributions". These may explain it (I don't know French). – Tomáš Cerha Jan 08 '18 at 12:18
  • Yes but `do_start(` is a function used to use program is in background. It is the next step of my install program. – Royce Jan 08 '18 at 12:21
  • I updated my question with the new `/etc/init.d/gobatch` file. – Royce Jan 08 '18 at 12:53
0

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
silenzi
  • 1
  • 1
  • I copy this code into my `gobatch` file, I start my daemon and check the status directly, problem is the same. – Royce Jan 08 '18 at 11:14
  • Writing a deamon is not that easy. This is why i linked a question which explains it instead of trying to explain it here. For example the deamon typically runs in the background and uses a PID file. – Tomáš Cerha Jan 08 '18 at 11:53