ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hello all,
I have an ubuntu 10.10 server and want to run a script on it to check if a process is running. If it is not running, it will start the process and also write into a log file.
When running the script i get the following error message:
syntax error near unexpected token `else'
Here is my script. Hope someone can help.
#!/bin/bash
process=`ps auxwww | grep test.x6 | grep -v grep | awk '{print $1}'`
if [ -z "$process" ]; then
echo "Couldn't find test.x86 running Restarting test Server" >>/var/testlog/test.check
echo && date >>/var/testlog/test.check
/emu/test/test.x86 -d >>/var/testlog/testdbg.log;
else echo "test.x86 is still OK!" >>/var/testlog/test.check
fi
Last edited by nav007_2000; 02-10-2011 at 04:46 PM.
Yes, and the semicolon before the else is not necessary. A cleaner way to test for the process would be to use pgrep. Is ps and grep all in one and it sets the return code based on a find. Here is its usage:
Code:
EXECUTABLE=/emu/test/test.x86
PROCESS=`basename $EXECUTABLE`
CHECK_FILE=/var/log/$PROCESS.check
LOG_FILE=/var/log/$PROCESS.log
pgrep $PROCESS > /dev/null
if [ $? -eq 1 ]; then
echo "Couldn't find $PROCESS running Restarting Server" >> $CHECK_FILE
date >> $CHECK_FILE
$EXECUTABLE &
else
echo "$PROCESS is still OK!" >> $CHECK_FILE
fi
your syntax looks ok. The semicolon/else issue is unusual but it should not throw an error. I see a windows logo in the left column of your post. Did you by any chance edit/create the file under windows? It might be some CR/LF issue. Execute the following command
Code:
od -c /path/to/your/script
Look out for suspicious character-sequencies like
\r\n
If it turns out that it is a CR/LF issue then you can correct this with
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.