Problems in running a script as a daemon at startup
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.???
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.