Results 1 to 10 of 10

Thread: shell script to scp last modified file

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default shell script to scp last modified file

    Hello Everyone,

    I have written below shell script to scp last modified log file.

    Code:
    cd /data/logs/apache/
    ls -trl | grep access-icecast.log.* | tail -1 | awk '{print $8}' | while read temp;
    do
            echo "This is before scp" > /tmp/before.txt
            scp $temp itmuser@192.168.201.138:/home/itmuser/XLMEDIA_Logs/jetedgede
            echo "This is after scp" > /tmp/after.txt
    done
    This script works fine when I directly execute it. However when I schedule this script under cron jobs, "scp $temp itmuser@192.168.201.138:/home/itmuser/XLMEDIA_Logs/jetedgede" part of the script does not work.

    Can anyone please suggest me what modification is required in the script so that it works when scheduled under cron jobs?


    Thank you.

  2. #2
    Senior Member
    Join Date
    Aug 2011
    Posts
    448
    Thanks
    0
    Thanked 62 Times in 58 Posts
    Rep Power
    10

    Default

    hi,

    «it does not work» does not provide much details.
    could you output stderr to a logfile to show how it does not work?

    may be it's because of unknown path to scp
    Code:
    cd /data/log/apache/
    /usr/bin/stat -c '%Y %n' access.icecast.log.* | /usr/bin/sort -r -k1,1n | { read date filename; /usr/bin/scp "$filename" itmuser@192.168.201.138:/home/itmuser/XLMEDIA_Logs/jetedgede;}

  3. #3
    Junior Member
    Join Date
    May 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default

    Hi Watael,

    Thank you for the reply.

    Script works fine if I manually execute it. My problem is, "SCP" statement is not running if I schedule it under cron jobs.

    Can you please explain the code which you posted?


    Thank you.

  4. #4
    Junior Member
    Join Date
    May 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default

    Hi Watael,

    Kindly find the attached document for the screenshot


    Thank you.

    crontab issue.zip

  5. #5
    Senior Member Rahul.Patil's Avatar
    Join Date
    Feb 2012
    Location
    Mumbai india
    Posts
    485
    Thanks
    10
    Thanked 49 Times in 46 Posts
    Rep Power
    8

    Default

    Hi,

    use absolute path of scp command , it should be /usr/bin/scp in CentOs5,
    use "which scp" command and check absolute path of scp
    Rahul Patil <http://www.linuxian.com>

  6. #6
    Senior Member
    Join Date
    Aug 2011
    Posts
    448
    Thanks
    0
    Thanked 62 Times in 58 Posts
    Rep Power
    10

    Default

    inside crontab, change >/dev/null 2>&1
    for >/data/scripts/copylog.log 2>&1
    so you'll be able to show us what's happening.

    chmod a+rx copylog.sh
    add a shebang, on the first line of your script : #!/bin/sh
    and call it without `sh`
    did you write your script in unix, or linux, or else?

    `ls` is painful to parse, so I preferably use `stat`, that outputs what I want.
    in my command line, `stat` prints
    %Y Time of last modification, seconds since Epoch
    %n File name
    piped to be sorted numerically in reverse order on first field only.
    then `read` gets first line inside two vars : date, and filename (obvious!)
    and finallly `scp` filename.
    braces are needed because right hand part of a pipe is executed inside a subscript; so the rest of the script would not know about created variables.
    such variables remain known inside braces.

  7. #7
    Junior Member
    Join Date
    May 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default

    Quote Originally Posted by Watael View Post
    inside crontab, change >/dev/null 2>&1
    for >/data/scripts/copylog.log 2>&1
    so you'll be able to show us what's happening.
    Like you suggested I have modified crontab and this what I see in the copylog.log file
    ssh: connect to host 21 port 22: Invalid argument


    Quote Originally Posted by Watael View Post
    chmod a+rx copylog.sh
    add a shebang, on the first line of your script : #!/bin/sh
    and call it without `sh`
    did you write your script in unix, or linux, or else?
    Modified the permissions of copylog.sh file and included #!/bin/sh on the first line of my script. I am writing this on Ubuntu server.

    Quote Originally Posted by Watael View Post
    `ls` is painful to parse, so I preferably use `stat`, that outputs what I want.
    in my command line, `stat` prints
    &#37;Y Time of last modification, seconds since Epoch
    %n File name
    piped to be sorted numerically in reverse order on first field only.
    then `read` gets first line inside two vars : date, and filename (obvious!)
    and finallly `scp` filename.
    braces are needed because right hand part of a pipe is executed inside a subscript; so the rest of the script would not know about created variables.
    such variables remain known inside braces.
    I tried running stat command but it's not giving any output.
    Code:
    root@jetedgede:/data/logs/apache# /usr/bin/stat -c '%Y %n' access.icecast.log.* | /usr/bin/sort -r -k1,1n
    /usr/bin/stat: cannot stat `access.icecast.log.*': No such file or directory
    root@jetedgede:/data/logs/apache# /usr/bin/stat -c '%Y %n' access.icecast.log.*
    /usr/bin/stat: cannot stat `access.icecast.log.*': No such file or directory
    root@jetedgede:/data/logs/apache# /usr/bin/stat -c '%Y %n' access.icecast.log.1336532400
    /usr/bin/stat: cannot stat `access.icecast.log.1336532400': No such file or directory
    root@jetedgede:/data/logs/apache#

    Thank you.

  8. #8
    Junior Member
    Join Date
    May 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default

    Quote Originally Posted by Rahul.Patil View Post
    use absolute path of scp command , it should be /usr/bin/scp in CentOs5,
    use "which scp" command and check absolute path of scp
    I used absolute path in my code, but in vain..

  9. #9
    Senior Member
    Join Date
    Aug 2011
    Posts
    448
    Thanks
    0
    Thanked 62 Times in 58 Posts
    Rep Power
    10

    Default

    yep, I miscopied filename, it's access-icecast.log.*

    also, always quote variables.

    there seems to be something wrong with scp/ssh, add a -v option to it, maybe it'll say more about what's wrong

  10. #10
    Junior Member
    Join Date
    May 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default

    Hi Watael,

    Thank you very much for your help.


    Quote Originally Posted by Watael View Post
    hi,

    &#171;it does not work&#187; does not provide much details.
    could you output stderr to a logfile to show how it does not work?

    may be it's because of unknown path to scp
    Code:
    cd /data/log/apache/
    /usr/bin/stat -c '&#37;Y %n' access-icecast.log.* | /usr/bin/sort -r -k1,1n | { read date filename; /usr/bin/scp "$filename" itmuser@192.168.201.138:/home/itmuser/XLMEDIA_Logs/jetedgede;}
    This code works perfectly under cron job as per my requirement.


    Regards,
    Ravi

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [Solved] Shell script to convert log file into a csv formatted text file
    By c00l_chintu in forum Shell scripting
    Replies: 13
    Last Post: 28th September 2012, 12:24 AM
  2. Script to FTP a modified file
    By PyroPlasm in forum Shell scripting
    Replies: 0
    Last Post: 20th January 2012, 11:30 PM
  3. Replies: 1
    Last Post: 25th August 2008, 02:39 PM
  4. Replies: 2
    Last Post: 31st May 2008, 06:44 PM
  5. If file modified execute a script
    By karabaja in forum Shell scripting
    Replies: 3
    Last Post: 23rd November 2006, 02:39 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
  •