Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Command not running in Linux script under a cron job

Posted on 2016-08-15
7
Medium Priority
?
86 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 9

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 9

Assisted Solution

by:James Bilous
James Bilous earned 2000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 9

Accepted Solution

by:
James Bilous earned 2000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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…
Suggested Courses
Course of the Month11 days, 11 hours left to enroll

916 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