Warning

 

Close
Confirm Action

Are you sure you wish to do this?

Cancel Confirm
AR15.COM
7/31/2014 7:55:56 AM EDT
ok Im trying to get the RPi to check and see if xterm is running and if its not restart it via a cron job.  

I understand cron and its layout. thats not the prob. its the shell script how do i get it to check the process running then respawn the xterm if dead?

ive tried this but it just gives me a bunch of syntax errors.

#!/usr/bin/bash
#set environment
logfile=/export/home/log/restart.log
PID=`ps -eo 'tty pid args' | grep 'xterm' | grep -v grep | tr -s ' ' | cut -f2 -d ' '`

if [ -z "$PID" ]
then
#Run the process <- I changed that to the proper location and var
echo "Started Process at `date`" >> $logfile
else
echo "Process is already Running with PIS=$PID"
fi

its blowing up at the log file, and PID when i comment out the log file string.

see bottom..

7/31/2014 8:04:22 AM EDT
[#1]
You're actually doing it right. I'm not sure why you're getting syntax errors.

Does /usr/bin/bash actually exist? It's not at /bin/bash?
7/31/2014 8:08:37 AM EDT
[#2]
You know, I think that's the problem. I don't have mine in front of me, but I'm 99.9% certain bash isn't located in /usr/bin/. That wouldn't make any sense. /usr is often a separate filesystem, and you'd want your shell to be on the root filesystem.

Change the first line to #!/bin/bash

You might also try 'whereis bash' at the command line, to see where it's located. I seriously doubt it's /usr/bin/bash.
7/31/2014 8:13:19 AM EDT
[#3]
ok the line marked as #Run the process <- I changed that to the proper location and var

is really /usr/bin/xt5250 iSeries

ill try the wheres bash and see what it says..

so it looks like this now..

#!/bin/bash
#set environment
logfile=/home/log/restart.log
PID=`ps -eo 'tty pid args' | grep 'xterm' | grep -v grep | tr -s ' ' | cut -f2 -d ' '`

if [ -z "$PID" ]
then
/usr/bin/xt5250 iSeries
echo "Started Process at `date`" >> $logfile
else
echo "Process is already Running with PIS=$PID"
fi

and I'm getting a unterminated quote now..
7/31/2014 8:15:42 AM EDT
[#4]
Quote History
Quoted:
You know, I think that's the problem. I don't have mine in front of me, but I'm 99.9% certain bash isn't located in /usr/bin/. That wouldn't make any sense. /usr is often a separate filesystem, and you'd want your shell to be on the root filesystem.

Change the first line to #!/bin/bash

You might also try 'whereis bash' at the command line, to see where it's located. I seriously doubt it's /usr/bin/bash.
View Quote


Or "which bash"

Check your environment with "echo $PATH"
7/31/2014 8:16:55 AM EDT
[#5]
its /bin/bash
7/31/2014 8:17:55 AM EDT
[#6]
Should the $PID be in quotation marks?  I'm thinking that would make it be acted on as text rather than a variable.
7/31/2014 8:23:53 AM EDT
[#7]
${PID}
7/31/2014 8:25:04 AM EDT
[#8]
7/31/2014 8:29:07 AM EDT
[#9]
GOT IT YES!

i had a single ' behind the log file that i missed seeing..

TY all!
7/31/2014 8:31:16 AM EDT
[#10]
Quote History


Beats actually working for a living.  
7/31/2014 8:31:49 AM EDT
[#11]
Quote History
Quoted:
GOT IT YES!

i had a single ' behind the log file that i missed seeing..

TY all!
View Quote



ITDCCVSR.  
7/31/2014 8:50:07 AM EDT
[#12]
this is actually really cool..

I got to say the Pi is a very fun board to play with.. and its making me learn linux again. might even try hacking on my MBP.. Since is all pretty much the same.
7/31/2014 8:54:06 AM EDT
[#13]
Quote History
Quoted:
this is actually really cool..

I got to say the Pi is a very fun board to play with.. and its making me learn linux again. might even try hacking on my MBP.. Since is all pretty much the same.
View Quote

There are some differences between the BSD and GNU versions of traditional UNIX programs, but they're easy to sort out.
7/31/2014 8:58:12 AM EDT
[#14]
If you can get in the habit of doing this, it will help avoid that problem of having the wrong path to your interpreter.

In vim, if you type '!!' followed by a command, the command output will be dropped into your file.  So if I know I'm writing a bash script, the first thing I do is type [ESC !! which bash] and the system tells me where bash is.  Then put [#!] in front of it and you're good to go.  

On systems where there may be non-standard installs of whatever interpreter, this skips the whole problem.  It's also useful for inserting today's date, etc.  

7/31/2014 9:05:07 AM EDT
[#15]
Quote History
Quoted:
If you can get in the habit of doing this, it will help avoid that problem of having the wrong path to your interpreter.

In vim, if you type '!!' followed by a command, the command output will be dropped into your file.  So if I know I'm writing a bash script, the first thing I do is type [ESC !! which bash] and the system tells me where bash is.  Then put [#!] in front of it and you're good to go.  

On systems where there may be non-standard installs of whatever interpreter, this skips the whole problem.  It's also useful for inserting today's date, etc.  

View Quote


Cool..
might have to pick up a book on script for my Hospital say next month..
7/31/2014 12:05:40 PM EDT
[#16]
ok. script runs.. but cron is wonkie..

i have a * * * * * rpitc /home/rpitc/restart.sh

It pops to the log file, saying its not started, DUH! but my /usr/bin/st5250 -s iSeries won't run from the /home/rpitc/startup.sh Does it need to be root in crontab and not rpitc?
7/31/2014 12:15:49 PM EDT
[#17]
Can that user run the command from the terminal?
7/31/2014 12:23:45 PM EDT
[#18]
The most common problem when trying to run a script from cron is that cron runs the script with no environment.  Specifically, it runs with no path variable.  So something like:

#!/bin/bash

echo "hello world!"

needs to be written as:  

#!/bin/bash

/bin/echo "hello world!"


if you're going to run it from cron.  

7/31/2014 12:57:49 PM EDT
[#19]
Quote History
Quoted:
Can that user run the command from the terminal?
View Quote


yes..

and it works..

its located in /usr/bin/xt5250 would that be /bin/sh( or bash) /usr/bin/xt5250?

it trys to pass off and the next line sends to the log file, its just not getting passed off to the other script (xt5250)

7/31/2014 1:10:57 PM EDT
[#20]
Quote History
Quoted:
The most common problem when trying to run a script from cron is that cron runs the script with no environment.  Specifically, it runs with no path variable.  So something like:

#!/bin/bashecho "hello world!"

needs to be written as:  

#!/bin/bash/bin/echo "hello world!" 


if you're going to run it from cron.  

View Quote


there is a path in crontab (with every body and its bro listed). so I'm totally lost as to whats wrong..
7/31/2014 1:35:40 PM EDT
[#21]
Here's what I wrote some time ago to accomplish the same thing.  The program that it checks up on drops a pid file in /var/run when it starts.  The program tries to clean up the pid file when it exits.  The script checks for it, if not found then just run.  If found, check for running process with same pid.  If not, delete pid file, kill anything that doesn't match, and start a new process.  

If you want to mimic this, start your xterm with a wrapper that drops the pid file.  


#!/usr/bin/perl -w

# mwatch - watch video gen process and restart on abort
#
# configuration
#------------------------------------------------------------------------------#
$mgserv = "/usr/local/bin/video/mgserv";
$pidfile = "/var/run/mgserv.pid";
#------------------------------------------------------------------------------#

# check for pid file
if (-e $pidfile) {
&RunCheck;
} else {
&Launch;
}

# subs
#------------------------------------------------------------------------------#
sub Launch {
system("$mgserv");
exit;
}

sub RunCheck {
open PID, "<$pidfile";
$pid = <PID>;
$runpid = `/sbin/pidof -x mgserv`;

if ("$pid" != "$runpid") {
system("/bin/rm -f $pidfile");
sleep 3;
system("/bin/killall -9 mgserv");
#system("/usr/bin/pkill mgserv");
&Launch;
}

}
7/31/2014 1:51:06 PM EDT
[#22]

Quote History
Quoted:
there is a path in crontab (with every body and its bro listed). so I'm totally lost as to whats wrong..
View Quote View All Quotes
View All Quotes
Quote History
Quoted:



Quoted:

The most common problem when trying to run a script from cron is that cron runs the script with no environment.  Specifically, it runs with no path variable.  So something like:




#!/bin/bashecho "hello world!"


needs to be written as:  




#!/bin/bash/bin/echo "hello world!" 




if you're going to run it from cron.  







there is a path in crontab (with every body and its bro listed). so I'm totally lost as to whats wrong..


What is that rpitc there?  Are you trying to run the command as the rpitc user?  If so, wouldn't you either a) put it in rpitc's cron tab or be have an 'su' command?  And no output redirection?



 
7/31/2014 1:52:53 PM EDT
[#23]
Quote History
Quoted:

What is that rpitc there?  Are you trying to run the command as the rpitc user?  If so, wouldn't you either a) put it in rpitc's cron tab or be have an 'su' command?  And no output redirection?
 
View Quote View All Quotes
View All Quotes
Quote History
Quoted:
Quoted:
Quoted:
The most common problem when trying to run a script from cron is that cron runs the script with no environment.  Specifically, it runs with no path variable.  So something like:

#!/bin/bashecho "hello world!"

needs to be written as:  

#!/bin/bash/bin/echo "hello world!" 


if you're going to run it from cron.  



there is a path in crontab (with every body and its bro listed). so I'm totally lost as to whats wrong..

What is that rpitc there?  Are you trying to run the command as the rpitc user?  If so, wouldn't you either a) put it in rpitc's cron tab or be have an 'su' command?  And no output redirection?
 

User. tried in users crontab as well. still no go.
7/31/2014 2:03:43 PM EDT
[#24]
Quote History
Quoted:
If you can get in the habit of doing this, it will help avoid that problem of having the wrong path to your interpreter.

In vim, if you type '!!' followed by a command, the command output will be dropped into your file.  So if I know I'm writing a bash script, the first thing I do is type [ESC !! which bash] and the system tells me where bash is.  Then put [#!] in front of it and you're good to go.  

On systems where there may be non-standard installs of whatever interpreter, this skips the whole problem.  It's also useful for inserting today's date, etc.  

View Quote

#!/usr/bin/env bash

is easier, and helps when you move from development to production and paths may be different.
7/31/2014 2:04:56 PM EDT
[#25]
For the life of me I don't know how anybody can do that for a living.
7/31/2014 2:06:36 PM EDT
[#26]
Quote History
Quoted:
For the life of me I don't know how anybody can do that for a living.
View Quote


me neither, Im about ready to chuck it across the room.  

Fuck this shit I'm going to work on some guns..
7/31/2014 2:07:34 PM EDT
[#27]
fffffuuuuuuck that shit.
7/31/2014 2:08:21 PM EDT
[#28]
Quote History
Quoted:
The most common problem when trying to run a script from cron is that cron runs the script with no environment.  Specifically, it runs with no path variable.  So something like:

#!/bin/bashecho "hello world!"

needs to be written as:  

#!/bin/bash/bin/echo "hello world!" 


if you're going to run it from cron.  

View Quote

Or you can define PATH at the beginning of your script:

PATH=/bin:/sbin:/usr/bin:/usr/sbin; export PATH
7/31/2014 2:10:25 PM EDT
[#29]
Quote History
Quoted:

User. tried in users crontab as well. still no go.
View Quote View All Quotes
View All Quotes
Quote History
Quoted:
Quoted:
Quoted:
Quoted:
The most common problem when trying to run a script from cron is that cron runs the script with no environment.  Specifically, it runs with no path variable.  So something like:

#!/bin/bashecho "hello world!"

needs to be written as:  

#!/bin/bash/bin/echo "hello world!" 


if you're going to run it from cron.  



there is a path in crontab (with every body and its bro listed). so I'm totally lost as to whats wrong..

What is that rpitc there?  Are you trying to run the command as the rpitc user?  If so, wouldn't you either a) put it in rpitc's cron tab or be have an 'su' command?  And no output redirection?
 

User. tried in users crontab as well. still no go.


If you're running xterm then don't you need a display to attach it to?  Not sure how to do that from cron.
7/31/2014 2:11:51 PM EDT
[#30]
Quote History
Quoted:
Here's what I wrote some time ago to accomplish the same thing.  The program that it checks up on drops a pid file in /var/run when it starts.  The program tries to clean up the pid file when it exits.  The script checks for it, if not found then just run.  If found, check for running process with same pid.  If not, delete pid file, kill anything that doesn't match, and start a new process.  

If you want to mimic this, start your xterm with a wrapper that drops the pid file.  


#!/usr/bin/perl -w
View Quote

Thanks for leaving an example of Why Perl Sucks.
7/31/2014 2:14:12 PM EDT
[#31]
Quote History
Quoted:


If you're running xterm then don't you need a display to attach it to?  Not sure how to do that from cron.
View Quote View All Quotes
View All Quotes
Quote History
Quoted:
Quoted:
Quoted:
Quoted:
Quoted:
The most common problem when trying to run a script from cron is that cron runs the script with no environment.  Specifically, it runs with no path variable.  So something like:

#!/bin/bashecho "hello world!"

needs to be written as:  

#!/bin/bash/bin/echo "hello world!" 


if you're going to run it from cron.  



there is a path in crontab (with every body and its bro listed). so I'm totally lost as to whats wrong..

What is that rpitc there?  Are you trying to run the command as the rpitc user?  If so, wouldn't you either a) put it in rpitc's cron tab or be have an 'su' command?  And no output redirection?
 

User. tried in users crontab as well. still no go.


If you're running xterm then don't you need a display to attach it to?  Not sure how to do that from cron.

enviro DISPLAY=:0,0 ?ill try that tomorrow..
7/31/2014 2:17:36 PM EDT
[#32]
Quote History
Quoted:

enviro DISPLAY=:0,0 ?ill try that tomorrow..
View Quote View All Quotes
View All Quotes
Quote History
Quoted:
Quoted:
Quoted:

User. tried in users crontab as well. still no go.


If you're running xterm then don't you need a display to attach it to?  Not sure how to do that from cron.

enviro DISPLAY=:0,0 ?ill try that tomorrow..


Worth a try.  Something like DISPLAY=:0 xterm should work as well
7/31/2014 2:20:22 PM EDT
[#33]
Quote History
Quoted:


If you're running xterm then don't you need a display to attach it to?  Not sure how to do that from cron.
View Quote View All Quotes
View All Quotes
Quote History
Quoted:
Quoted:
Quoted:
Quoted:
Quoted:
The most common problem when trying to run a script from cron is that cron runs the script with no environment.  Specifically, it runs with no path variable.  So something like:

#!/bin/bashecho "hello world!"

needs to be written as:  

#!/bin/bash/bin/echo "hello world!" 


if you're going to run it from cron.  



there is a path in crontab (with every body and its bro listed). so I'm totally lost as to whats wrong..

What is that rpitc there?  Are you trying to run the command as the rpitc user?  If so, wouldn't you either a) put it in rpitc's cron tab or be have an 'su' command?  And no output redirection?
 

User. tried in users crontab as well. still no go.


If you're running xterm then don't you need a display to attach it to?  Not sure how to do that from cron.

You export DISPLAY=:0 (or whatever X display) before starting whatever program.

If you're starting a process that would normally open a window in X but you don't need to see it or click in it, you can use Xvfb to let the app bind to a virtual DISPLAY. This works great when you need to use something like Selenium or PhantomJS drive Firefox or Chrome to scrape data off of websites that dynamically display content via JavaScript. Using Xvfb like this allows you to avoid installing a full-blown X server on a web server.
7/31/2014 2:25:17 PM EDT
[#34]


       
Quote History
Quoted:





User. tried in users crontab as well. still no go.

View Quote View All Quotes
View All Quotes
Quote History
Quoted:



Quoted:


Quoted:


Quoted:

The most common problem when trying to run a script from cron is that cron runs the script with no environment.  Specifically, it runs with no path variable.  So something like:




#!/bin/bashecho "hello world!"


needs to be written as:  




#!/bin/bash/bin/echo "hello world!" 




if you're going to run it from cron.  







there is a path in crontab (with every body and its bro listed). so I'm totally lost as to whats wrong..


What is that rpitc there?  Are you trying to run the command as the rpitc user?  If so, wouldn't you either a) put it in rpitc's cron tab or be have an 'su' command?  And no output redirection?

 


User. tried in users crontab as well. still no go.



OK, I normally do ksh instead of bash, but a typical crontab for me will be something like this:



* * * * * /usr/local/scripts/whatever.ksh > /tmp/whatever.log 2>&1