Solved

Command not running in Linux script under a cron job

Posted on 2016-08-15
7
56 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 7

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 7

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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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 7

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
The purpose of this article is to demonstrate how we can use conditional statements using Python.
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.

762 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now