Find the answer to your Linux question:
Results 1 to 3 of 3
Hi folks, I am very new to Linux. I want to set up a cron job to execute a bash script, below is what I think I shoul do after ...
  1. #1
    Just Joined!
    Join Date
    Nov 2006
    Posts
    1

    How to set up cron job to execute bash script

    Hi folks,

    I am very new to Linux. I want to set up a cron job to execute a bash script, below is what I think I shoul do after searching stuff on the Internet. I'd be much appreciated if anyone could point me out what is incorrect and anything else I need to do.

    The scenario is SSH (I have access details) to the server, set up a cron job to execute a bash script at 3am every 48 hours. The bash script meant to change system current working directory to the one I specify and invoke a php script.

    1. create a bash script search.txt (text file) which contains the following command. I want this bash script switch current working dicrectory to the one i specified below and invoke file sphider.php

    Code:
    #!/bin/bash
    cd /home/gwcwater/public_html/sphider/admin
    php spider.php full
    2. I think I need to upload this bash script to somewhere on the server. is this correct? Where is the appropriate folder to place this file?

    3. create a text file, contab.txt, which contains,

    Code:
    0 3 */2 * * [i know i need to specify the bash script file path here, but not sure how]
    4. upload crontab.txt using ASCII mode (where is the appropriate palce to place this file on the server?)

    5. SSH to the server

    6. cd to the directory where contab.txt is stored

    7. execute command: crontab contab.txt

  2. #2
    Just Joined!
    Join Date
    Jun 2007
    Posts
    9
    I'm no pro. but you may need to chmod +x you're file crontab.txt.
    Also I've never named a script with a .txt extension. Can't promise it will help, but it's worth a try.

  3. #3
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    Where you place the script is really up to you. I suggest you set up a directory to hold scripts like this:

    # mkdir /home/gwcwater/scripts

    Place the script there.

    frippee is correct. After you upload the file you have to make it executable.

    Your crontab entry would look like this:

    0 3 */2 * * /home/gwcwater/scripts/search.txt >/home/gwcwater/search.log 2>&1

    The stuff to the right starting with the '>' directs all output to a file, in this case it's search.log in your home directory. The '2>&1' redirects standard error '2 or stderr' to standard output, which is 1 or stdout. For clarity the crontab entry could also be:

    0 3 */2 * * /home/gwcwater/scripts/search.txt 1>/home/gwcwater/search.log 2>&1

    File extensions (for the most part) are meaningless in Linux so it won't matter if your script file ends end with .txt. For clarity maybe you should use the extension .bash instead.

    Vic

Posting Permissions

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