Solved

Command not running in Linux script under a cron job

Posted on 2016-08-15
7
80 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 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
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.
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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

752 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