LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Problems in running a script as a daemon at startup (https://www.linuxquestions.org/questions/programming-9/problems-in-running-a-script-as-a-daemon-at-startup-320759/)

fahad26 05-06-2005 11:20 AM

Problems in running a script as a daemon at startup
 
HI

I want to run a bash script which is as follows at startup:


#!/bin/sh
#
# chkconfig: 345 85 15
# Source function library
. /etc/rc.d/init.d/functions

case "$1" in
start)
echo -n "Starting update: "
daemon update
echo touch /var/lock/subsys/update

lab=1
rm -f /home/exporte/*
rm -f /home/localwork/*
rm -f /home/currentuser/*
#An infinite loop. The problem is somewhere here
while test $lab="1"
do
cp -u /home/localwork/* /home/currentuser
cp -u /home/exporte/* /home/u02062
done
;;
stop)
echo -n "Shutting down update :"
killproc update
echo update
rm -f /var/log/subsys/update
;;
*)
echo "usage: update.init {start|stop}"
exit 1
esac
exit 0


Now after placing this script in /etc/rc.d/init.d and doing chkconfig --add update when i restart the computer, my service update runs.

The problem is that due to the infinite loop that i want to execute in the script, the system doesnt moves forward and no other service is activated and i dont get the logon screen.

Please tell me how can i run this script at startup so that this script is always running on my computer and also the system loads all the service and i am able to do login properly.

rstewart 05-06-2005 11:44 AM

Hi,

A very simple technique that I have used is to simply code your endless loop functionality into a second script or executable. From within the primary script that is run during init you invoke (execute) the second script as a background program ie "/bin/sh second_script.sh &". This will cause the primary script to fork/exec the secondary script while the primary script runs to completion and terminates properly.

oblivious69 05-06-2005 12:34 PM

where are you changing the value of $lab? it always seems to be 1 and stuck in the while loop.

Arnoldrenjith 03-07-2012 05:38 AM

Problem in daemon startup script
 
HI

I have tried to run a startup script as daemon


#!/bin/sh
#
# chkconfig: 345 85 15
# Source function library
. /etc/rc.d/init.d/functions

case "$1" in
start)
echo -n "Starting update: "
daemon update ---->> daemon "test.sh" #This is the script i want to make as daemon
echo touch /var/lock/subsys/update

lab=1
rm -f /home/exporte/*
rm -f /home/localwork/*
rm -f /home/currentuser/*
#An infinite loop. The problem is somewhere here
while test $lab="1"
do
cp -u /home/localwork/* /home/currentuser
cp -u /home/exporte/* /home/u02062
done
;;
stop)
echo -n "Shutting down update :"
killproc update
echo update
rm -f /var/log/subsys/update
;;
*)
echo "usage: update.init {start|stop}"
exit 1
esac
exit 0

THis is not getting converted to a daemon process.Can we provide /etc/init.d/myScript as argument to 'daemon'; or only bindary files can be given.???

Arnoldrenjith 03-07-2012 06:25 AM

Problem in daemon startup script
 
HI

I have tried to run a bash script as daemon using 'daemon <shell script.sh>'


#!/bin/sh
#
# chkconfig: 345 85 15
# Source function library
. /etc/rc.d/init.d/functions

case "$1" in
start)
echo -n "Starting update: "
daemon "test.sh" #This is the script i want to make as daemon
echo touch /var/lock/subsys/test

lab=1
rm -f /home/exporte/*
rm -f /home/localwork/*
rm -f /home/currentuser/*
#An infinite loop. The problem is somewhere here
while test $lab="1"
do
cp -u /home/localwork/* /home/currentuser
cp -u /home/exporte/* /home/u02062
done
;;
stop)
echo -n "Shutting down test.sh:"
killproc test.sh
echo test.sh
rm -f /var/log/subsys/test
;;
*)
echo "usage: test.sh.init {start|stop}"
exit 1
esac
exit 0

While executing, this is not getting converted to a daemon process.Can we provide /etc/init.d/myScript.sh as argument to 'daemon';
OR only binary files can be given.???

We are observing that the if we try to invoke any binary with 'daemon <binaryName>' it is getting daemonized.
Also if it is invoked with 'daemon <shell script> &', it starts as a daemon process.

Appreciate any help...

Thanks

catkin 03-07-2012 07:24 AM

As oblivious69 pointed out in post 3 of this thread, the script never increments $lab so the test $lab="1" is always true (after substitution it is "1=1" which is a non-empty string so always tests true) and the loop never completes.

Even if you changed $lab in the loop it would never exit because the equality operator = must have spaces on either side.


All times are GMT -5. The time now is 02:03 PM.