vForums Support > Programming & Coding :: Programming Discussion :: > SHELL Programming Help

SHELL Programming Help - Posted By dog199200 (dog199200) on 30th Mar 10 at 6:48am
OK for a while now I have been running my server in Windows Server 2008, well I finally changed it over to Ubuntu Desktop. My problem is that I have several batch scripts that have to be converted over... The problem I that I have no idea how to convert them so I looked it up, and upon doing so, I found that there is no GoTo function in SHELL, and almost all my batch scripts use a go to functions. I have converted one of my scripts the best I can, but I can't get it to work, so can someone please look at the code and tell me what I have done wrong converting it?

Code:
 
  1. #! /bin/ksh
  2.  
  3. tasklist /FI "IMAGENAME eq Server.exe" 2> /dev/null | find /I /N "Server.exe"> /dev/null
  4.  
  5. if [ $? = 1  ]
  6. then
  7.     echo "<FONT COLOR=#00DD00><B>ONLINE</B></FONT>" > status.txt
  8. else
  9.     echo "<FONT COLOR=#DD0000><B>OFFLINE</B></FONT>" > status.txt
  10. fi
 


I have no idea how tasklist works in SHELL, but the ERRORLEVEL ($1) function isn't even working.

Edit: If the codes isn't clear enough, i want the file to check to see if the server.exe is running. If it is it will say the "then" option, and if it isn't, then it will say the "else" option. Before when I did this code I used goto and if and if not with the error level functions:


Code:
 
  1. tasklist /FI "IMAGENAME eq Server.exe" 2>NUL | find /I /N "Server.exe"> NUL
  2. IF ERRORLEVEL 1 goto Process_NotFound
  3. IF NOT ERRORLEVEL 1 goto Process_Found
  4.  
  5. :Process_Found
  6. echo "<FONT COLOR=#00DD00><B>ONLINE</B></FONT>" > status.txt
  7. goto END
  8.  
  9. :Process_NotFound
  10. echo "<FONT COLOR=#DD0000><B>OFFLINE</B></FONT>" > status.txt
  11. goto END
  12.  
  13. :END
 

Re: SHELL Programming Help - Posted By Ross (admin) on 30th Mar 10 at 8:19am
This is a snippet of a script I run to check if apache2 is running. Not sure what the differences would be with Ubuntu desktop.

Code:
 
  1. #!/bin/bash
  2.  
  3. a=$(ps cax | grep -c ' apache2$')
  4.  
  5. if [ $a -ge 1 ]; then
  6.         echo "Apache is Running :)";
  7. else
  8.         echo "Apache is NOT running"
  9. fi
 

Re: SHELL Programming Help - Posted By dog199200 (dog199200) on 30th Mar 10 at 8:36am
ok so in general, would iot be posisble that something like this would work?

Code:
 
  1. #!/bin/sh
  2.  
  3. if ps ax | grep -v grep | grep Server.exe > /dev/null
  4. then
  5.     echo "<FONT COLOR=#00DD00><B>ONLINE</B></FONT>" > status.txt
  6. else
  7.     echo "<FONT COLOR=#DD0000><B>OFFLINE</B></FONT>" > status.txt
  8. fi
 


I got the coding example from here: http://www.anyexample.com/linux_bsd/bash/check_if_program_is_running_with_bash_shell_script.xml


Edit: OK some what related question, I know when using the terminal to set a cronjon you use crontab -e to bring up the text menu to submit the cronjob. I currently have 5 **** /Path/To/status.sh but for some reason its not working. I want it to run the script every 5 minutes, so based on the docs I have found it should be right {Unsure}

Re: SHELL Programming Help - Posted By Ross (admin) on 30th Mar 10 at 9:50am
That cron would tell it to run at 5 past the hour. You'd want:

*/5 * * * * /path/to/status.sh

Or you may need:

*/5 * * * * /path/to/bash /path/to/status.sh


As for the code, it's only a snippet I got from somewhere. My shell skills leave much to be desired. have you tried:

Code:
 
  1. #!/bin/bash
  2.  
  3. a=$(ps cax | grep -c ' Server.exe$')
  4.  
  5. if [ $a -ge 1 ]; then
  6.          echo "<FONT COLOR=#00DD00><B>ONLINE</B></FONT>" > status.txt
  7. else
  8.         echo "<FONT COLOR=#DD0000><B>OFFLINE</B></FONT>" > status.txt
  9. fi
 

Re: SHELL Programming Help - Posted By dog199200 (dog199200) on 30th Mar 10 at 7:42pm
hmm the crontab still wont work {Unsure}

also I got the script to now output the text file, the i dont currently have the server program on my pc, so i used apache2 to test and it worked.

Re: SHELL Programming Help - Posted By Ross (admin) on 30th Mar 10 at 10:16pm
Posted By dog199200 on 30th Mar 10 at 7:42pm
 
hmm the crontab still wont work {Unsure}

also I got the script to now output the text file, the i dont currently have the server program on my pc, so i used apache2 to test and it worked.


Can you run /bin/bash /path/to/status.sh via the command line?

Re: SHELL Programming Help - Posted By dog199200 (dog199200) on 30th Mar 10 at 11:07pm
yea I can, thats how I know it works, but when i set it as a crontab, well it wont process.

Side question, you mentioned using apache2, and i was wondering if you could possible give me an idea how to set it up right {Unsure} I can only access the site from within my local network, though when trying to go outside the network it say the site dont exist. I have been following multiple guides and so far i've only been making things worse XD

Re: SHELL Programming Help - Posted By Ross (admin) on 31st Mar 10 at 10:36am
Posted By dog199200 on 30th Mar 10 at 11:07pm
 
yea I can, thats how I know it works, but when i set it as a crontab, well it wont process.

Side question, you mentioned using apache2, and i was wondering if you could possible give me an idea how to set it up right {Unsure} I can only access the site from within my local network, though when trying to go outside the network it say the site dont exist. I have been following multiple guides and so far i've only been making things worse XD


Are you using a router on your network? You will need to change the settings on it to forward port 80 to your machine.

It's also possible that the connection is getting blocked by your firewall.

Re: SHELL Programming Help - Posted By dog199200 (dog199200) on 31st Mar 10 at 10:51am
{Wink} trust me, that is not the problem {Tongue Out} i have DMZ setup and router to my server. Network wise, nothing has changed since I was running Win Server 2008, though maybe its a firewall within the system? I'm not sure how linux works or anything like that so I have no idea if it has its own internal firewall.