Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Command not running in Linux script under a cron job

Posted on 2016-08-15
7
Medium Priority
?
82 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
7 Extremely Useful Linux Commands for Beginners

Just getting started with Linux? Here's a quick start guide that has 7 commands that we believe will come in handy.

 

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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

730 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