CRON script help!

Posted:
in Genius Bar edited January 2014
Im told i can run script at certain times in the day without having to execute it by hand every time.



Id like to run



cd /Volumes/INETPUB

find . -name "._*" -exec rm '{}' \\; -print



at say 10am and 4pm monday to friday.



How do i make a 'cron' script for this.



please note i a very basic user of the termnal!!



cheers



Mark

Comments

  • Reply 1 of 2
    ok, first, make absolutely sure that the script is written correctly. try it out once or twice without the 'rm' part. maybe with 'echo' so you can see exactly everything that would've been affected.



    now that your script is perfect, the next step is to identify what program or shell would best be fitted to executing it. nothing special is done, so i would recommend using /bin/sh. it will work with almost everthing matching /bin/*sh (almost any shell can handle that script). 'sh' is copy of bash, with is a pretty small shell.



    now, copy and paste your final script into a new text document. i recommend using emacs, pico, nano or vi, because they don't interpret the files, they just use em as raw text. text edit can also save as raw files, but then ur extension is going to be .txt. now, copy this line onto the very first line of your file:

    Code:


    # ! /bin/sh





    you can replace '/bin/sh' with whatever shell you like (like '/bin/tcsh'). now save that somewhere familliar, like ~, or ~/Desktop. in terminal, make that file executable with chmod:

    Code:


    chmod a+x fileName;





    where fileName is the full path to your newly saved file (like, ~/Desktop/myFile). now put the script inside /usr/local/bin:

    Code:


    sudo mkdir -p /usr/local/bin

    sudo cp fileName /usr/local/bin/







    now, whever you want to run that script, you type '/usr/local/bin/fileName'. next step is cron:



    cron uses files called crontabs. the main one is stored in '/etc/crontab'. so pick an editor (i like emacs), and open that file with superuser privileges:

    Code:


    sudo emacs /etc/crontab





    now, on a new line at the bottom, write this:

    Code:


    0 10,4 * * 2-5 root /usr/local/bin/fileName





    (Make sure there is one extra line after the end of that line.) Here's an explanation: The first number (0), is the minute passed the hour. (0-based, anything from 0-59). The second (10,4) is the hour(s) (comma-separated list, no spaces, 0-23). The third field is the day of the month (* means match any day of the month, as long as the other fields match; 0-31 is valid). the next is the month (*, similarly, means disregard this field in the search; 0-11). "2-5" refers to the day of the week (1=Sunday, 2=Monday .. 5=Friday, 6=Saturday, 7=Sunday). 'root' is the user to execute the program, and /usr/local/bin/fileName is the program to execute. All fields can be comma separated bunches, and can be inclusive ranges (even both), and can also be the 'glob' (*).
  • Reply 2 of 2
    dobbydobby Posts: 797member
    You could also

    $ su - (change to root)

    $ crontab -e

    add the line



    0 10,4 * * 2-5 find /Volumes/INETPUB -name "._*" -exec rm '{}' \\; -print



    sorted!



    Dobby.
Sign In or Register to comment.