Results 1 to 8 of 8
  1. #1
    Join Date
    Dec 2004
    Location
    Behind You.
    Posts
    692

    Question Anyone know how to program in Shell ?

    I'm trying to program a very simple shell script that does 2-3 things.

    1) checks for the number of a script running say, "ps aux | grep php | wc -l" returns that number.

    2) deletes temp files folders "cd /to/that/directory/; rm -fr *&" every 90 seconds IF that number in check #1 is below say 50....

    and then have this shell script launched in shell every so often, not sure on the frequency but first is how to program in shell is a TIME DELAY.

    #!/bin/sh

    $x = 0;

    while($x!=1)
    {
    echo "cd /to/that/directory/; rm -fr *&";
    delay(90000); // 90 x 1000 milli seconds

    if("ps aux | grep php | wc -l" > 50)
    {
    $x=1; // to end script
    }

    }
    now i'm 99% sure this won't work in shell, it's basically how I would express it in alogorithm and would almost run in php but i'd like to use shell for this I think.

    Any suggestions/help?

    Thanks in advance

  2. #2
    May i ask what is this script for? To check if some hack software running?

    If so, i would reccomend better to configure mod_sec and make /var/tmp folder non-executable. This will prevent 95% of problems.

  3. #3
    Join Date
    Jun 2003
    Location
    UK
    Posts
    6,601
    Optimising this a bit try replacing

    "ps aux | grep php | wc -l" with "pgrep -c php"

    Also I would replace

    "cd /to/that/directory/; rm -fr *&" with "cd /to/that/directory/ && rm -fr *&"

    This means that it will only run rm -rf if the cd actually works. If that directory is missing rm -rf * could easily nuke your root file system
    Russ Foster - Industry Curmudgeon

  4. #4
    Join Date
    Dec 2004
    Location
    Behind You.
    Posts
    692
    but is the syntax of the script correct? I was 100% guessing when I wrote it real quick, it's like what I would call a hybrid of php coding, I program in php all the time only and haven't ever coded anything in shell really.

    could I wouldn't use cd /, I would use

    echo "rm -fr /to/that/directory/* &";

    instead, thanks for reminding me however, i may have forgot if you guys said it was perfect. but do you guys code in shell and is there any problems with the coding or syntax that won't work in shell? I haven't ran it yet obviously.

    nope, it's not to check for some hack thing running, it's for some custom stuff I developed in php and I want to launch it every so often but also want to keep it under control, if it doesn't finish normally i'll end up with too many running and bring the box down.

    I haven't completely done the last 5% in my head about this yet, I want this to be running in the background 100% of the time or something I think, yea....

    I need it to run to say k=1000 and each time clear a tmp directory in /home/user$k/public_html/tmp_files/

    I want the temp dir to be cleared every 24 hours basically, and if i launched rm -fr /dirctory/* & ; 1000 times at the same time, i brought the box down trying to write all over the hard drive at the same time, lol, so basically I need this script to wait 90 seconds before incrementing to delete the next temp directory?

    does that make sense, that was the thought with the delay(90000); but that's php code, I don't know the equivalents for all my script above in shell....

    Thanks in advance for help with finishing this script hopefully....

  5. #5
    Join Date
    Feb 2005
    Location
    Australia
    Posts
    5,842
    Your code does look more like php than shell script. I suggest you check out one of the many online tutorials to help you with the syntax.
    Chris

    "Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them." - Laurence J. Peter

  6. #6
    Join Date
    Apr 2003
    Location
    UK
    Posts
    2,560
    its not shell script. try this:

    Code:
    #!/bin/sh
    
    while [ "$x" != 5 ];
    do
      echo $x
      x=`expr $x + 1`
    done
    
    echo "x = $x"
    your commands will have to be in backticks (eg `ps aux | grep php | wc -l`)

    and so the syntax of your 'if' wont work either

    you'll need something like

    Code:
    if [ `ps aux | grep php | wc -l` > 50 ];
    do
      break;
    done
    cant remember if the break will break the while or not - its been a while (id do this in perl!)

  7. #7
    Join Date
    Dec 2004
    Location
    Behind You.
    Posts
    692
    alright thanks, i'll take a crack at it tonight and post back how far i get

  8. #8
    Join Date
    Aug 2005
    Location
    Canada
    Posts
    838
    Use cron to launch it every few minutes instead of letting it stay with looping.
    Code:
    #!/bin/sh
    
    if  [ `ps aux | grep php | wc -l` -gt  50 ] ; then
      rm -fr "/to/that/directory/"
    fi
    mkdir "/to/that/directory/"
    I changed it a bit.
    (Deleting the directory, instead of listing many many files first with "*", and then deleting them all).

    (Not tested)
    Last edited by extras; 10-12-2006 at 11:37 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •