LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 01-05-2010, 06:06 AM   #1
dEM0nsTAr
LQ Newbie
 
Registered: Nov 2009
Posts: 28

Rep: Reputation: 15
Question Bash script to toggle a daemon (here proftpd)


I want to create a script which toggles a daemon (here proftpd). On the first execution it should start it and on the next one it should stop it but first check if proftpd is already running or not (and make the decision to start or stop on that).
I have created this script:

Code:
#!/bin/sh
# proftpd-toggle

if pgrep proftpd > /dev/null; then
exec /etc/init.d/proftpd stop
else
exec /etc/init.d/proftpd start
fi
but it always stop proftpd and never starts it (even if pgrep proftpd is empty).
Where is the problem and how can I solve it?
Thanks in advance.
 
Old 01-05-2010, 06:12 AM   #2
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
I think the problem is in /etc/init.d/proftpd. Also why do you have to use exec? What is the header of /etc/init.d/proftpd, '#!/bin/sh' too?
 
Old 01-05-2010, 06:22 AM   #3
dEM0nsTAr
LQ Newbie
 
Registered: Nov 2009
Posts: 28

Original Poster
Rep: Reputation: 15
I am not sure but it looks like this and it works if I try to start/stop it manually with /etc/init.d/proftpd start/stop:

Code:
#!/bin/sh

case $1 in
'start' )
/opt/sbin/proftpd
;;
'stop' )
kill `ps -ef | grep proftpd | grep -v grep | awk '{print $1}'` > /dev/null 2>&1
;;
*)
echo "usage: $0 {start|stop}"
esac
If I use the following script only the "echo run" is being displayed and after that a "Terminated", none of the others:

Code:
#!/bin/sh
# proftpd-toggle
echo run
if pgrep proftpd > /dev/null; then
exec /etc/init.d/proftpd stop
echo start
else
exec /etc/init.d/proftpd start
echo stop
fi
echo end
Quote:
~# /home/user/apps/proftpdtoggle.sh
run
Terminated

Last edited by dEM0nsTAr; 01-05-2010 at 06:24 AM.
 
Old 01-05-2010, 06:30 AM   #4
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
You really shouldn't use exec for that.
 
Old 01-05-2010, 06:44 AM   #5
dEM0nsTAr
LQ Newbie
 
Registered: Nov 2009
Posts: 28

Original Poster
Rep: Reputation: 15
I have deleted the exec commands but it is still the same problem:

Quote:
~# /home/user/apps/proftpdtoggle.sh
run
Terminated
 
Old 01-05-2010, 06:58 AM   #6
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
'grep proftpd' might also be matching your script '/home/user/apps/proftpdtoggle.sh' that's why it's terminated before echoing start or stop.

try to add '-w' to grep.

maybe same as 'pgrep proftpd' that's why it always see a process?

Last edited by konsolebox; 01-05-2010 at 07:00 AM.
 
Old 01-05-2010, 07:21 PM   #7
dEM0nsTAr
LQ Newbie
 
Registered: Nov 2009
Posts: 28

Original Poster
Rep: Reputation: 15
The installed grep version doesn't support -w and another strange thing is that the script stops right after the stop of proftpd (it doesn't even display the echo command in the next line of the same if-statement).
isn't there another solution for a toggle script?
 
Old 01-05-2010, 08:35 PM   #8
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,014

Rep: Reputation: 115Reputation: 115
Try using -x for pgrep.
 
1 members found this post helpful.
Old 01-05-2010, 08:47 PM   #9
GooseYArd
Member
 
Registered: Jul 2009
Location: Reston, VA
Distribution: Slackware, Ubuntu, RHEL
Posts: 183

Rep: Reputation: 46
does proftpd write a pidfile? grepping for pids is prone to failure.
 
Old 01-05-2010, 08:55 PM   #10
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by dEM0nsTAr View Post
The installed grep version doesn't support -w
Maybe you're using busybox.
Quote:
Originally Posted by dEM0nsTAr View Post
and another strange thing is that the script stops right after the stop of proftpd (it doesn't even display the echo command in the next line of the same if-statement).
It could always stop probably because as I said, pgrep and/or grep also matches the script so the script is also killed when 'kill' is executed.

If in doubt you can let the output of grep and pgrep go to stdin
Code:
if pgrep proftpd; then  # removed /dev/null

...

#!/bin/sh

case $1 in
'start' )
/opt/sbin/proftpd
;;
'stop' )
ps -ef | grep proftpd   # added this one
kill `ps -ef | grep proftpd | grep -v grep | awk '{print $1}'` > /dev/null 2>&1
;;
*)
echo "usage: $0 {start|stop}"
esac
Quote:
Originally Posted by dEM0nsTAr View Post
isn't there another solution for a toggle script?
if grep -w is not usable, can we also just use pgrep for that?... maybe we can also just use awk
Code:
kill `ps -ef | awk '{ $2 == "proftpd" { print $1; }; }'`

Last edited by konsolebox; 01-05-2010 at 08:56 PM.
 
1 members found this post helpful.
Old 01-06-2010, 03:00 AM   #11
dEM0nsTAr
LQ Newbie
 
Registered: Nov 2009
Posts: 28

Original Poster
Rep: Reputation: 15
With your changes the output looks like this:
Code:
Nokia-N900-42-11:~# /home/user/apps/proftpdtoggle.sh
3421
stop
 3421 root      2088 S    /bin/sh /home/user/apps/proftpdtoggle.sh
 3423 root      2084 S    /bin/sh /etc/init.d/proftpd stop
 3425 root      2088 S    grep proftpd
Terminated
And yes, I am using Busybox here because the script is for a Nokia N900.
Where should I use your last code?
 
Old 01-06-2010, 11:05 AM   #12
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 4,143

Rep: Reputation: 638Reputation: 638Reputation: 638Reputation: 638Reputation: 638Reputation: 638
maybe this will yeild better mileage (also consider scp/ ssh:
Code:
#!/bin/bash
# proftpd-toggle

if [ -z "`pgrep proftpd | tee ~/proftp.test`" ]
then
 sudo /etc/init.d/proftpd start | tee ~/proftp.test
else
 sudo /etc/init.d/proftpd stop | tee ~/proftp.test
fi
also consider proftpd_pid=$! to store the pid of the previous process.


edit: also are you in the us ?
where did you get your n900/ how much ?
what cell company do you have ?
is it gsm and cdma or just gsm ?

thanks,

Last edited by schneidz; 01-06-2010 at 11:20 AM.
 
Old 01-07-2010, 02:17 AM   #13
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
as suggested, you can use 'pgrep -x'

Code:
#!/bin/sh

# proftpd-toggle

if pgrep -x proftpd > /dev/null; then
	exec /etc/init.d/proftpd stop
else
	exec /etc/init.d/proftpd start
fi
Code:
#!/bin/sh

case "$1" in
start)
	/opt/sbin/proftpd
	;;
stop)
	kill `ps -ef | awk '{ $2 == "proftpd" { print $1; }; }'` > /dev/null 2>&1
	;;
*)
	echo "usage: $0 {start|stop}"
	;;
esac
shouldn't you just also use pgrep instead of ps when killing the process?
Code:
kill `pgrep -x proftpd'` > /dev/null 2>&1

Last edited by konsolebox; 01-07-2010 at 02:20 AM.
 
1 members found this post helpful.
Old 01-07-2010, 03:50 AM   #14
dEM0nsTAr
LQ Newbie
 
Registered: Nov 2009
Posts: 28

Original Poster
Rep: Reputation: 15
pgrep -x did the trick, thanks!
What is wrong with
Code:
#!/bin/sh

case $1 in
'start' )
/opt/sbin/proftpd
;;
'stop' )
ps -ef | grep proftpd
kill `ps -ef | grep proftpd | grep -v grep | awk '{print $1}'` > /dev/null 2>&1
;;
*)
echo "usage: $0 {start|stop}"
esac
Code:
#!/bin/sh
# proftpd-toggle
if pgrep -x proftpd > /dev/null; then
sudo /etc/init.d/proftpd stop > /dev/null
else
echo Started
sudo /etc/init.d/proftpd start
fi
exit 0
??? It works exactly as it should. Isn't it reliable or safe?
 
Old 01-07-2010, 08:17 PM   #15
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by dEM0nsTAr View Post
pgrep -x did the trick, thanks!
I think tuxdev is the one worthy of that.
Quote:
Originally Posted by dEM0nsTAr View Post
What is wrong with
Code:
#!/bin/sh

case $1 in
'start' )
/opt/sbin/proftpd
;;
'stop' )
ps -ef | grep proftpd
kill `ps -ef | grep proftpd | grep -v grep | awk '{print $1}'` > /dev/null 2>&1
;;
*)
echo "usage: $0 {start|stop}"
esac
Code:
#!/bin/sh
# proftpd-toggle
if pgrep -x proftpd > /dev/null; then
sudo /etc/init.d/proftpd stop > /dev/null
else
echo Started
sudo /etc/init.d/proftpd start
fi
exit 0
??? It works exactly as it should. Isn't it reliable or safe?
Not just counting that it also includes the script itself when looking for processes, it probably is also less efficient and yes probably less reliable.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 06:23 PM
Run my bash script as a daemon. jaimese Linux - Newbie 12 02-10-2011 04:28 PM
[SOLVED] Help with simple script to toggle gamma values on lcd monitor musonio Linux - Software 14 09-25-2009 01:44 AM
Bash script to verify the daemon is working if not, start the daemon kishoreru Linux - Newbie 7 09-23-2009 05:29 AM
bash: daemon: command not found linx win Fedora 2 05-07-2007 09:27 PM


All times are GMT -5. The time now is 04:00 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration