LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices


Reply
  Search this Thread
Old 07-30-2004, 06:21 PM   #1
wenberg
Member
 
Registered: Jan 2004
Location: Minnesota, USA
Distribution: Slackware 10, Debian Sarge
Posts: 91

Rep: Reputation: 15
rc.local and cron questions


I can start programs on boot by putting commands in rc.local. My question is, how do start programs on boot under a different user other than root?

My second question is ... where can I get info on making a script that I can use in cron. I want it to check if a program is running, and if not ... run a program.

Thanks for any help.
 
Old 07-30-2004, 07:32 PM   #2
Mephisto
Member
 
Registered: Oct 2002
Location: Washington D.C, USA
Distribution: Slack 12, Etch, Gutsy
Posts: 453

Rep: Reputation: 31
Quick answer

su - <username> -c "<command>"

Longer answer read "man su" and also read "man chmod" and more specifically for "X" set user ID for execution.

For cron look in the /etc/cron.* directories, there might be a sample there. If not I am sure someone here could provide an example. Basically you could grep for the process and if it was not found start it.
 
Old 07-31-2004, 10:46 AM   #3
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
anybody know how to make that bash script??

the one that checks to see if something is running and starts something if it's not...

=)
 
Old 07-31-2004, 11:03 AM   #4
320mb
Senior Member
 
Registered: Nov 2002
Location: pikes peak
Distribution: Slackware, LFS
Posts: 2,577

Rep: Reputation: 48
Quote:
Originally posted by win32sux
anybody know how to make that bash script??

the one that checks to see if something is running and starts something if it's not...

=)
ya easy...........

http://www.tldp.org/LDP/Bash-Beginne...ers-Guide.html
http://www.ibiblio.org/pub/Linux/doc...tro-HOWTO.html
 
Old 07-31-2004, 11:28 AM   #5
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
okay, let me re-phrase the question:

can somebody please post an example script that checks to see if something is running and starts something if it's not???

 
Old 07-31-2004, 12:52 PM   #6
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
okay i'll use pseudo-code and hopefully someone can change this to a real bash script:


ps aux | grep example.bin | grep -v grep

if the command above gives any output, then example.bin is running, so let's just exit...

if the command above doesn't give any output, then example.bin isn't running, so let's run it...

 
Old 07-31-2004, 01:42 PM   #7
Mephisto
Member
 
Registered: Oct 2002
Location: Washington D.C, USA
Distribution: Slack 12, Etch, Gutsy
Posts: 453

Rep: Reputation: 31
Bash scripting is not really my strongpoint. That said a simple example would be

if ps -e | grep -q "<executable name>"; then
echo "executable already running"
else
<executable command>
fi

I usually have to tinker a bit before I get the script right though.
 
Old 07-31-2004, 03:25 PM   #8
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
Quote:
Originally posted by Mephisto
Bash scripting is not really my strongpoint. That said a simple example would be

if ps -e | grep -q "<executable name>"; then
echo "executable already running"
else
<executable command>
fi

I usually have to tinker a bit before I get the script right though.
awesome! thanks, man!

=)



i blended my piece with your script and got this:

Code:
#!/bin/sh
if /usr/bin/ps aux | grep example.bin | grep -qv grep; then
exit
else /usr/bin/example.bin
fi
it seems to work very well... what do you think??

i have a couple questions:

what's the "-e" option on "ps" do?? is it better to use that instead of "aux"??

would it be wise to put an ampersand at the end of line 4 to make sure the example.bin process goes to the background??


Last edited by win32sux; 07-31-2004 at 03:55 PM.
 
Old 07-31-2004, 04:01 PM   #9
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
i added a couple lines so that it will log whenever it confirms the process is running, and whenever it starts another process...

Code:
#!/bin/sh
if /usr/bin/ps aux | grep example.bin | grep -qv grep; then
/usr/bin/date >> /var/log/checker.log
echo "example.bin CONFIRMED" >> /var/log/checker.log
exit
else /usr/bin/example.bin
/usr/bin/date >> /var/log/checker.log
echo "example.bin INITIATED" >> /var/log/checker.log
fi
how can i log the date on the same line as "example.bin CONFIRMED/INITIATED"??

with the script above the log looks like this:

Code:
Sat Jul 31 15:59:49 EST 2004
example.bin INITIATED
Sat Jul 31 16:00:49 EST 2004
example.bin CONFIRMED
Sat Jul 31 16:01:49 EST 2004
example.bin CONFIRMED

Last edited by win32sux; 07-31-2004 at 04:09 PM.
 
Old 07-31-2004, 04:16 PM   #10
SBing
Member
 
Registered: Mar 2004
Posts: 519

Rep: Reputation: 35
Quote:
Originally posted by win32sux
what's the "-e" option on "ps" do?? is it better to use that instead of "aux"??
You can find out a lot about commands by just type '--help' after a command, try:

$ ps --help

You'll receive an output of (cropped):

-e all processes

For scripting it'll make little difference I believe.
Quote:
Originally posted by win32sux
how can i log the date on the same line as "example.bin CONFIRMED/INITIATED"??
Try replacing:

echo "example.bin CONFIRMED" >> /var/log/checker.log

with

echo `date` "example.bin CONFIRMED" >> /var/log/checker.log

You can customize how the date is outputted to your own chosen format, try:

$ date --help

for more details

Good luck!
 
Old 07-31-2004, 04:24 PM   #11
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
that worked great! thanks SBing!

Code:
#!/bin/sh
if /usr/bin/ps aux | grep example.bin | grep -qv grep; then
echo `/usr/bin/date` "example.bin CONFIRMED" >> /var/log/checker.log
exit
else /usr/bin/example.bin
echo `/usr/bin/date` "example.bin INITIATED" >> /var/log/checker.log
fi

Last edited by win32sux; 07-31-2004 at 04:26 PM.
 
Old 07-31-2004, 04:32 PM   #12
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
is it best to use entire paths or just the command?? why??

echo `date` "example.bin CONFIRMED" >> /var/log/checker.log

echo `/usr/bin/date` "example.bin CONFIRMED" >> /var/log/checker.log


Last edited by win32sux; 07-31-2004 at 04:41 PM.
 
Old 07-31-2004, 04:52 PM   #13
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
That depends of the environment try put echo $PATH >> /var/log/checker.log you will see if /usr/bin is into it, no needs to indicate /usr/bin/date, just date will do
 
Old 07-31-2004, 06:29 PM   #14
Mephisto
Member
 
Registered: Oct 2002
Location: Washington D.C, USA
Distribution: Slack 12, Etch, Gutsy
Posts: 453

Rep: Reputation: 31
In Linux ps -e might not make a difference, I didn't check. On Irix it does though. Since I bounce around a lot (Irix, AIX, QSH, and BSD, but predominently Linux) I tend to use commands that work relatively consistently across platforms. Thus the -e.
 
Old 07-31-2004, 06:47 PM   #15
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
Quote:
Originally posted by Mephisto
In Linux ps -e might not make a difference, I didn't check. On Irix it does though. Since I bounce around a lot (Irix, AIX, QSH, and BSD, but predominently Linux) I tend to use commands that work relatively consistently across platforms. Thus the -e.
just curious, what's QSH???
 
  


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
cron questions darkleaf Debian 2 06-21-2005 01:23 PM
cron job questions.... chunlee Linux - Software 1 02-18-2005 05:51 AM
Two cron questions downinthemine Linux - Software 5 01-11-2004 02:35 PM
rc.local and cron background questions Greg21 Linux - Software 1 06-29-2003 11:46 AM
Just Questions..... OSS, cron, inetd, etc ?? nautilus_1987 Linux - General 12 11-28-2002 11:31 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware

All times are GMT -5. The time now is 09:22 PM.

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