Solved

Command not running in Linux script under a cron job

Posted on 2016-08-15
7
76 Views
Last Modified: 2016-08-15
Hi there,

I created a script (oracle-copy.sh) that retrieves the most recent ZIP file and then stores the name of it in a variable.  After it does that, it runs a "scp" command to copy that ZIP file to a remote server.  When I run the script manually, it does the copy just fine, but when I set this script to run under a cron job every day, it seems to fail at the command that retrieves the latest ZIP file name.  

This commands retrieves the name: LATESTFILE=$(ls *.zip -t1 | head -n1)
I can tell that it doesn't get the file name because I then run this command and the name is not attached: echo "/data99/backup/$LATESTFILE".  It just shows me /data99/backup/ with nothing else.

Thank you in advance,
- Christian
0
Comment
Question by:Christian Palacios
  • 4
  • 3
7 Comments
 
LVL 8

Expert Comment

by:James Bilous
ID: 41756609
Are you looking in the correct folder? Maybe try providing an absolute path to the folder that should contain the zip files.
0
 

Author Comment

by:Christian Palacios
ID: 41756614
Here is the entry in the crontab file:
45 22 * * * /data99/backup/oracle-copy.sh >> /var/log/cyphertite.log
The script is in the same location as the ZIP files.

When I view cyphertite.log, all I get is this:
Starting backup of file  to file /dbdump/diskos_prod.zip
/data99/backup/
0
 
LVL 8

Assisted Solution

by:James Bilous
James Bilous earned 500 total points
ID: 41756755
When you run a script it creates a new subshell, so the context of the script will be that of the user running the script in a brand new shell. For example, if you are usually in your home folder when you open a new shell, running a script located in ~/a/folder/script.sh which does an "ls" command will show the contents of your home folder. You need to put in the full path to your file in the script or change directory to there first.
0
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 

Author Comment

by:Christian Palacios
ID: 41756761
Thank you.  Here is the content of the .sh file.  I am putting the full path so maybe just a CD first as you suggested?

#!/bin/sh
#
LATESTFILE=$(ls *.zip -t1 | head -n1)

#Display message about starting the backup
echo "Starting backup of file $LATESTFILE"

scp /data99/dbdump/backup/$LATESTFILE oracle@10.44.28.16:/dbdump/diskos_prod.zip
echo "/data99/dbdump/backup/$LATESTFILE"

Thanks!
0
 
LVL 8

Accepted Solution

by:
James Bilous earned 500 total points
ID: 41756764
Right, the problem is

LATESTFILE=$(ls *.zip -t1 | head -n1)

Open in new window


being executed in the wrong context.

LATESTFILE=$(ls /data99/dbdump/backup/*.zip -t1 | head -n1)

Open in new window


should work (assuming all the zip files are in /data99/dbdump/backup), or you can change directory to that folder before running the ls command.
0
 

Author Comment

by:Christian Palacios
ID: 41756795
Ah, that was it!!  Thanks very much James.  I'll close this question.

- Christian
0
 

Author Closing Comment

by:Christian Palacios
ID: 41756797
Perfect solution!
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question