LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-06-2005, 11:20 AM   #1
fahad26
LQ Newbie
 
Registered: Jan 2005
Location: Pakistan
Distribution: Red Hat
Posts: 17

Rep: Reputation: 0
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.
 
Old 05-06-2005, 11:44 AM   #2
rstewart
Member
 
Registered: Feb 2005
Location: Sunnyvale, CA
Distribution: Ubuntu
Posts: 205

Rep: Reputation: 38
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.
 
Old 05-06-2005, 12:34 PM   #3
oblivious69
Member
 
Registered: May 2005
Distribution: debian
Posts: 79

Rep: Reputation: 15
where are you changing the value of $lab? it always seems to be 1 and stuck in the while loop.
 
Old 03-07-2012, 05:38 AM   #4
Arnoldrenjith
LQ Newbie
 
Registered: Mar 2012
Posts: 2

Rep: Reputation: Disabled
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.???
 
Old 03-07-2012, 06:25 AM   #5
Arnoldrenjith
LQ Newbie
 
Registered: Mar 2012
Posts: 2

Rep: Reputation: Disabled
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
 
Old 03-07-2012, 07:24 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script running as daemon Satriani Programming 12 03-29-2011 05:23 PM
Running autorun daemon on startup basemodel Linux - Software 4 08-20-2004 04:31 PM
reconfigure snort daemon startup script hari_seldon99 Linux - Security 1 05-18-2004 01:40 AM
Running script as daemon StinkyFinger Linux - Newbie 2 08-28-2003 07:31 PM
syslog startup daemon script jpflager Linux - Software 6 03-05-2003 12:59 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:35 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration