Search Members Help

» Welcome Guest
[ Log In :: Register ]

Mini-ITX Boards Sale, Fanless BareBones Mini-ITX, Bootable 1G DSL USBs, 533MHz Fanless PC <-- SALE $200 each!
Get The Official Damn Small Linux Book. DSL Market , Great VPS hosting provided by Tektonic
Pages: (3) </ [1] 2 3 >/

[ Track this topic :: Email this topic :: Print this topic ]

reply to topic new topic new poll
Topic: Simple shell-based daemon, More a template than useful in itself.< Next Oldest | Next Newest >
the Missing M Offline





Group: Members
Posts: 35
Joined: Mar. 2007
Posted: May 11 2007,03:11 QUOTE

Well it's clunky, but it works.  I'm probably unduly proud of it, because this is my first attempt at a `structured' sh script.  Anything before that was just a simple list of commands, which could just as easily be typed into a terminal.  Of course, they have saved me the trouble of typing those same lines over and over again.

My main reason for writing it was to perform a few basic actions as root, without getting hassled for a password every time.  Not really an issue in DSL, at least not from the live CD.

You don't necessarily need to have the referenced apps installed, but it might come in handy for automating other things you'd like to automate, whether they require root privs or not.  That's why I'm posting it here.  Oh, and because I'm unduly proud of it.  ;-)

Code Sample
#!/bin/sh

echo Starting r00t_l00p...

#  This script adds some super-user functionality to regular
#  old Wendy Whitebread accounts.
#
#  What it does is check for certain directory names in /tmp,
#  then acts on whatever it finds there.  So other programs, and
#  the user, can communicate with it via `mkdir' or a launcher
#  button that runs a simple `mikdir /tmp/r00t/foo' command.

mkdir -p /tmp/r00t/run
chmod -R 777 /tmp/r00t
sleep 11
rm -rf /tmp/r00t/run
#
#  If the `run' folder exists, this indicates that a r00t script
#  has just been started, and other instances should quit now.
#
#  Avoids multiple instances piling up from one login to the next.


while :
do
       if [ -x /tmp/r00t/run ]
       then
               echo Ending r00t_l00p.
               exit 0
               #  What did I just say?



       elif [ -x /tmp/r00t/shutdown ]
       ####  Unused, but I might need these sometime...
       then
               rm -rf /tmp/r00t/shutdown
               killall sleep
               sudo -u moi xmms -s
               sleep 2
               shutdown -P now "Thassal, Folks!"

       elif [ -x /tmp/r00t/reboot ]
       then
               rm -rf /tmp/r00t/reboot
               killall sleep
               sudo -u moi xmms -s
               sleep 2
               shutdown -r now "Back in a bit..."



       elif [ -x /tmp/r00t/hibernate ]
       ####  But I do need this, because the standard
       ####  `Hibernate' button never works in Xubuntu...
       then
               rm -rf /tmp/r00t/hibernate
               sudo -u moi xmms -s
               sleep 1
               umount /dev/sda1

               /etc/acpi/hibernate.sh
               #
               #  Requires the addition of an `&' in
               #  /etc/acpi/hibernate.sh
               #
               #  Here, on the last line;
               #  . /etc/acpi/resume.sh &
               #
               #  Otherwise, the hibernate script will just sit there
               #  and wait, long after it's finished its work, and so
               #  will r00t_l00p; waiting for hibernate.sh to exit.


               #  And on resume;
               #
               sudo -u moi mount /dev/sda1
               mount /dev/sda1
               sleep 7
               sudo -u moi xmms -p



       elif [ -x /tmp/r00t/insomnia ]
       ####  Makes `sleep'-deferred actions happen now
       ####  In other words; `Procrastinate Later'.
       then
               rm -rf /tmp/r00t/insomnia
               killall sleep



       elif [ -x /tmp/r00t/ftp-stat ]
       ####  Server status
       then
               rm -rf /tmp/r00t/ftp-stat

               if [ -x /tmp/r00t/ftp-on ]
               then
                       xterm -title '::0N::' \
                       -geometry 28x3 -e 'echo && echo \
                       "     FTP Server is ON."        && echo -n \
                       "                           "   && sleep 3' &
                       #
                       #  Woohoo!  GUI notification dialogs
                       #  from a shell script!

               elif [ -x /tmp/r00t/ftp-off ]
               then
                       xterm -title '::0FF::' \
                       -geometry 28x3 -e 'echo && echo \
                       "     FTP Server is OFF."       && echo -n \
                       "                           "   && sleep 3' &

               #       "     [press any key to "       && echo -n \
               #       "       close window]      "    && read -n 1' &

               fi



       elif [ -x /tmp/r00t/ftp-switch ]
       ####  Turn the server on or off
       then
               rm -rf /tmp/r00t/ftp-switch

               if [ -x /tmp/r00t/ftp-on ]
               ####  If it's on, stop it
               then
                       xterm -title '::0FF::' \
                       -geometry 28x3 -e 'echo && echo \
                       "    Stopping FTP Server."      && echo -n \
                       "                           "   && sleep 3' &

                       mv /tmp/r00t/ftp-on /tmp/r00t/ftp-off

                       /etc/init.d/proftpd stop
                       /etc/init.d/proftpd force-stop
                       sleep 3
                       killall -s 9 proftpd

               elif [ -x /tmp/r00t/ftp-off ]
               ####  If it's off, start it
               then
                       xterm -title '::0N::' \
                       -geometry 28x3 -e 'echo && echo \
                       "    Starting FTP Server."      && echo -n \
                       "                           "   && sleep 3' &

                       mv /tmp/r00t/ftp-off /tmp/r00t/ftp-on

                       /etc/init.d/proftpd start

               elif [ -x /tmp/r00t ]
               ####  And if you don't know, stop it anyway
               then
                       mkdir -p /tmp/r00t/ftp-off

                       /etc/init.d/proftpd stop
                       /etc/init.d/proftpd force-stop
                       sleep 3
                       killall -s 9 proftpd
               fi
       fi

sleep 3
done


Probably insecure as hell, because it does rely on a world-writable directory to take instructions, but that's fine with me because the only remote file access I've got going is FTP, and even that is usually turned off [does come in handy though, for transferring files between a Mac, PC and my currently favourite Linux box].  Besides, it's all sitting behind a router.

However, since this is a laptop, I do want to be sure the FTP daemon's really, truly and completely shut down when I head out to an internet cafe or something.



Blah, foo, bar, etc [and please excuse another long post],

Patrick.


--------------
Q: What is the difference between
     a joke, and a lie?
A: A lie tends to obscure the truth,
     while a joke often reveals it.
Back to top
Profile PM 
humpty Offline





Group: Members
Posts: 655
Joined: Sep. 2005
Posted: May 11 2007,06:03 QUOTE

Quote (the Missing M @ May 11 2007,07:11)
However, since this is a laptop, I do want to be sure the FTP daemon's really, truly and completely shut down..

you can do a grep on a ps to a temp file. if the file is emtpy, then it's a gonna.
Back to top
Profile PM 
the Missing M Offline





Group: Members
Posts: 35
Joined: Mar. 2007
Posted: May 11 2007,07:17 QUOTE

Quote (humpty @ May 10 2007,19:03)
you can do a grep on a ps to a temp file. if the file is emtpy, then it's a gonna.

AHA!  ps -e|grep proftpd > ~/foo

Thanks.  :-)

And I finally figured out how to get an `if [[ $this = $that ]]' statement working properly.

Code Sample
x=`ps -e|grep proftpd`
if [[ $x = "" ]]; then echo phoo; else echo $x; fi


[just some quick tests, jotted down in the terminal]

But maybe I should rephrase my original statement; I don't want to make sure anything is properly shut down, or properly launched.  I want something else to do it for me.

But now it can report, accurrately, whether the thing's running or not, instead of just counting on the daemon being properly configured, and launching when it's told to launch [always has so far, but still...].  I have enough faith in killall -s 9 not to worry about that, but in most cases [probably every case] that's really overdoing it.



Thanks again,

Patrick.


--------------
Q: What is the difference between
     a joke, and a lie?
A: A lie tends to obscure the truth,
     while a joke often reveals it.
Back to top
Profile PM 
the Missing M Offline





Group: Members
Posts: 35
Joined: Mar. 2007
Posted: May 19 2007,13:13 QUOTE

Just a quick revision, in case anyone finds this useful.  When I first wrote it, I thought the `-x' test was short for `exists' btw...  Kind of lucked out though, because directories also tend to be executable.

And thanks again to Humpty, for the note on ps -e | grep [foo] .
Code Sample
#!/bin/sh

echo Starting r00t_l00p...

#  This script adds some super-user functionality to regular
#  old Wendy Whitebread accounts.
#
#  What it does is check for certain directory names in /tmp,
#  then acts on whatever it finds there.  So other programs, and
#  the user, can communicate with it via `mkdir' or a launcher
#  button that runs a simple `mkdir /tmp/r00t/foo' command.

mkdir -p /tmp/r00t/run
chmod -R 777 /tmp/r00t
sleep 11
rm -rf /tmp/r00t/run
#
#  If the `run' folder exists, this indicates that a r00t script
#  has just been started, and other instances should quit now.
#
#  Avoids multiple instances piling up from one login to the next.


while :
do
sleep 2
       if [ -d /tmp/r00t/run ]
       then
               echo Ending r00t_l00p.
               exit 0
               #  What did I just say?



       elif [ -d /tmp/r00t/shutdown ]
       ####  Unused, but I might need these sometime...
       then
               rm -rf /tmp/r00t/shutdown
               killall sleep
               sudo -u moi xmms -s
               sleep 2
               shutdown -P now "Thassal, Folks!"

       elif [ -d /tmp/r00t/reboot ]
       then
               rm -rf /tmp/r00t/reboot
               killall sleep
               sudo -u moi xmms -s
               sleep 2
               shutdown -r now "Back in a bit..."



       elif [ -d /tmp/r00t/hibernate ]
       ####  But I do need this, because the standard
       ####  `Hibernate' button never works in Xubuntu...
       then
               rm -rf /tmp/r00t/hibernate
               sudo -u moi xmms -s
               sleep 1
               umount /dev/sda1

               /etc/acpi/hibernate.sh
               #
               #  Requires the addition of an `&' in
               #  /etc/acpi/hibernate.sh
               #
               #  Here, on the last line;
               #  . /etc/acpi/resume.sh &
               #
               #  Otherwise, the hibernate script will just sit there
               #  and wait, long after it's finished its work.  And so
               #  will r00t_l00p; waiting for hibernate.sh to exit.


               #  And on resume;
               #
               sudo -u moi mount /dev/sda1
               mount /dev/sda1
               sleep 7
               sudo -u moi xmms -p



       elif [ -d /tmp/r00t/insomnia ]
       ####  Makes `sleep'-deferred actions happen now
       then
               rm -rf /tmp/r00t/insomnia
               killall sleep



       elif [ -d /tmp/r00t/ppp-switch ]
       then
               rm -rf /tmp/r00t/ppp-switch

               if [ "`ps -e | grep pppd`" = "" ]
                       then pon Primus.ca
                       else poff -a
               fi



       elif [ -d /tmp/r00t/ftp-stat ]
       ####  Server status
       then
               rm -rf /tmp/r00t/ftp-stat

               if [ "`ps -e | grep proftpd`" = "" ]
               then
                       xterm -title '::0FF::' \
                       -geometry 28x3 -e 'echo; echo \
                       "     FTP Server is OFF."      ; echo -n \
                       "                           "  ; sleep 2' &
                       #
                       #  Woohoo!  GUI notification
                       #  dialogs from the shell!

               else
                       xterm -title '::0N::' \
                       -geometry 28x3 -e 'echo; echo \
                       "     FTP Server is ON."        ; echo -n \
                       "                           "  ; sleep 2' &

               #       "     [press any key to "      ; echo -n \
               #       "        close window]     "    ; read -n 1' &

               fi



       elif [ -d /tmp/r00t/ftp-switch ]
       ####  Turn the server on or off
       then
               rm -rf /tmp/r00t/ftp-switch

               if [ "`ps -e | grep proftpd`" = "" ]
               ####  If it's off, start it
               then
                       xterm -title '::0N::' \
                       -geometry 28x3 -e 'echo; echo \
                       "    Starting FTP Server."      ; echo -n \
                       "                           "  ; sleep 3' &

                       /etc/init.d/proftpd start
                       sleep 2
                       if [ "`ps -e | grep proftpd`" = "" ]
                       then
                               xterm -title '::FA1L::' \
                               -geometry 28x6 -e 'echo; echo \
                               "  Server would not start."; echo; echo \
                               "      [press any key"  ; echo \
                               "        to proceed]"  ; echo -n \
                               "                           "; read -n 1'

                               sh -c 'cd /etc/init.d; \
                               xfce4-terminal -T "F1X PR0FTPD"' &
                       fi

               else
               ####  If it's on, stop it

                       xterm -title '::0FF::' \
                       -geometry 28x3 -e 'echo; echo \
                       "    Stopping FTP Server."      ; echo -n \
                       "                           "  ; sleep 3' &

                       /etc/init.d/proftpd stop
                       sleep 1
                       if [ "`ps -e | grep proftpd`" = "" ]
                       then
                               xterm -title '::0FF::' \
                               -geometry 28x3 -e 'echo; echo \
                               "    Stopped FTP Server."      ; echo -n \
                               "                           "  ; sleep 3' &
                       else
                               /etc/init.d/proftpd force-stop
                               sleep 1
                               if [ "`ps -e | grep proftpd`" = "" ]
                               then
                                       xterm -title '::0FF::' \
                                       -geometry 28x3 -e 'echo; echo \
                                       "    Stopped FTP Server."      ; echo -n \
                                       "                           "  ; sleep 3' &
                               else
                                       killall -s 9 proftpd
                                       sleep 1
                                       if [ "`ps -e | grep proftpd`" = "" ]
                                       then
                                               xterm -title '::0FF::' \
                                               -geometry 28x3 -e 'echo; echo \
                                               "    Stopped FTP Server."      ; echo -n \
                                               "                           "  ; sleep 3' &
                                       else
                                               xterm -title '::FA1L::' \
                                               -geometry 28x6 -e 'echo; echo \
                                               "     WTF?   Unkillable..."    ; echo; echo \
                                               "     [press any key to"        ; echo \
                                               "        close window]"; echo -n \
                                               "                           "  ; read -n 1' &
                                       fi
                               fi
                       fi
               fi
       fi
done


The progresion from `stop' to `force-stop' to `killall -s 9' gets a bit convoluted, but as far as I know it's never progressed past the basic `stop'.  Well, not in normal use anyway.  I did launch GProFTPD [a graphical configuration tool for the daemon] to test it.

BTW, does anyone know how to make grep match only whole words, and ignore word fragments like the `proftpd' in `gproftpd'?  I've messed with some of its command-line options, but haven't guesed right yet.



Thanks,

Patrick.


--------------
Q: What is the difference between
     a joke, and a lie?
A: A lie tends to obscure the truth,
     while a joke often reveals it.
Back to top
Profile PM 
mikshaw Offline





Group: Members
Posts: 4856
Joined: July 2004
Posted: May 19 2007,13:41 QUOTE

Quote
BTW, does anyone know how to make grep match only whole words, and ignore word fragments like the `proftpd' in `gproftpd'?

Try using spaces within quotes or after backshlashes:
grep " gproftpd "
or
grep \ gproftpd\ (<< a space)


--------------
http://www.tldp.org/LDP/intro-linux/html/index.html
Back to top
Profile PM WEB 
14 replies since May 11 2007,03:11 < Next Oldest | Next Newest >

[ Track this topic :: Email this topic :: Print this topic ]

Pages: (3) </ [1] 2 3 >/
reply to topic new topic new poll
Quick Reply: Simple shell-based daemon

Do you wish to enable your signature for this post?
Do you wish to enable emoticons for this post?
Track this topic
View All Emoticons
View iB Code