Find the answer to your Linux question:
Results 1 to 6 of 6
Hi friends, I execute the script as;- */5 * * * * script_name It gives me the wrong o/p. But when I execute the script thru command line:- bash script_name ...
  1. #1
    Just Joined!
    Join Date
    Sep 2009
    Posts
    4

    How to Execute bash script & triggers email

    Hi friends,

    I execute the script as;-
    */5 * * * * script_name
    It gives me the wrong o/p.

    But when I execute the script thru command line:-
    bash script_name
    It give me the perfect result.

    I have given 777 permissions all throughout to the files this script refers to but still not able to make out what's wrong.
    Any idea?

  2. #2
    Linux Newbie
    Join Date
    Jul 2008
    Posts
    181
    What is "o/p" supposed to mean?

    Have you checked that the script itself is executable? Have you checked that the script is in the path used by cron?

  3. #3
    Just Joined!
    Join Date
    Sep 2009
    Posts
    4

    reply

    O/p is output.
    well the script I have given 777 permission.
    Also, the files it is referring to , i tried them giving 777 as well but still not working.
    pls help!

  4. #4
    Just Joined!
    Join Date
    Sep 2009
    Posts
    4

    this is my script

    pls help ASAP

  5. #5
    Linux Newbie
    Join Date
    Mar 2009
    Posts
    228
    One thing that many people don't realise is that when a script runs in cron the account's profile script is NOT run 1st like it is when you log in. So things like PATH most likely aren't defined as they are in your interactive shell.

    Best to change the shebang line to:

    #!/bin/bash -vx

    to see what is happening.

    One of the best things to do to avoid problems is specify the full path for commands you execute.

    Instead of:

    command <options>

    use:

    /path/to/command <options>

  6. #6
    Just Joined!
    Join Date
    Sep 2009
    Posts
    4

    script looks like:-

    Clear
    exec 1> servicecheck_status
    echo "This is an Automated Alert !!"
    period=`date`
    echo "Today is $period"
    echo " "

    ##command to check the service(& re-directing the output to file "u001"):-
    service status 2> u001

    ##logging on to the other server to check the same service with same command:-
    ssh server_IP service status 2> u002

    result_001=`cat u001`
    result_002=`cat u002`

    ##"re_stored" is the file which it refers to while making decisions
    result_stored=`cat re_stored`

    if [[ "$result_001" = "$result_stored" && "$result_002" = "$result_stored" ]]
    then
    echo "The services are STOPPED on both the servers.Please fix it immediately by starting it on any of the servers."
    mail -s "Automated Alert for Services" -r servicecheck@mail.com myemailadd.com < servicecheck_status

    elif [[ "$result_001" != "$result_stored" && "$result_002" != "$result_stored" ]]
    then
    echo "The services are RUNNING on both the servers.Please fix it immediately by stopping it on any of the servers."
    mail -s "Automated Alert for Services" -r servicecheck@mail.com myemailadd.com < servicecheck_status
    else
    echo "FINE" > mohit.log
    fi
    ##When everything is fine it will not send the alert but create a file "mohit.log"
    ================================================== ===
    above is the script which I made for checking the services on 2 servers, comparing the alerts & then sending the alerts accordingly.
    Now when I run this script like through command line:-
    $ bash script_name
    It runs perfect;

    However when I put it in cron like :-
    */15 * * * * /path/to/script
    It does not run as it should.

    I tried giving 777 permissions to the script as well the file it refers to ("re_stored") compare the alerts, still it fails.
    Now tell me where m I going wrong??
    Please HELP!

Posting Permissions

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