LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-11-2016, 06:16 AM   #1
Ujio
LQ Newbie
 
Registered: Sep 2011
Posts: 17

Rep: Reputation: Disabled
shell script does not run properly from cron daemon


Hello

I have a small script like that

Quote:
#!/bin/bash

i=$(/bin/find <path> -name "*.csv" -mmin -15 | /usr/bin/wc -l)

if test $i -eq 0
then
#do something about file not found
else
#do something about file found
fi

The <path> directory has some csv files that modified in last 15 mins. When I run this script from command line there is no issue, it finds the files and do something about file found
But this script could not find anything if it initiated from cron daemon. I checked many times files are always exists in my trials

I tried to change directory to <path> before find command but nothing changed. It's really smalll script but could not find out my error. I need to run this script from cron daemon

Could you help me about it ?
Ujio

Last edited by Ujio; 07-11-2016 at 06:23 AM.
 
Old 07-11-2016, 06:27 AM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
You are sure code is not executed in else condition from cron?
I mean the issue could be in "do something about file found" block when executed with cron
 
Old 07-11-2016, 06:34 AM   #3
Ujio
LQ Newbie
 
Registered: Sep 2011
Posts: 17

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by keefaz View Post
You are sure code is not executed in else condition from cron?
I mean the issue could be in "do something about file found" block when executed with cron
Hello

I checked it many ways,
When the script initated from cron, $i was always 0 there is no problem with if-then-else block.
Therefore I addedd full path of the commands, tried to change directory into <path> and then run find command but no progress

So I am assuming my problem is in following line

Quote:
i=$(/bin/find <path> -name "*.csv" -mmin -15 | /usr/bin/wc -l)
But could not find what is my error

thanks for your reply
Ujio
 
Old 07-11-2016, 06:37 AM   #4
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
For testing, I would check output from find command in cron
Code:
#!/bin/bash
output=/tmp/find_cron.txt

/bin/find <path> -name "*.csv" -mmin -15 > $output
 
Old 07-11-2016, 07:01 AM   #5
kilgoretrout
Senior Member
 
Registered: Oct 2003
Posts: 2,987

Rep: Reputation: 388Reputation: 388Reputation: 388Reputation: 388
If it runs from the command line but not from cron, it's usually a problem with a path in the script. Cron scripts run in a different environment than your normal bash shell and and an abbreviated path that may be decipherable in your normal bash shell may not be in the cron environment. Try changing your "<path>" to a full path. Don't assume that your cron script is starting from the same place as your bash shell.
 
Old 07-11-2016, 07:56 AM   #6
Ujio
LQ Newbie
 
Registered: Sep 2011
Posts: 17

Original Poster
Rep: Reputation: Disabled
I think it was garbage character problem.
I added your recommendations to top of the script, it has started to run correctly then I disabled your recommendations it is still running correctly now I removed your recommendations. It is still running correctly.

But I could not figured out why it was running properly from command line

Thanks for your answers
Ujio
 
Old 07-13-2016, 12:32 AM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
For debugging in future, try

Code:
#!/bin/bash
set -xv
Shows exactly what the parser sees / is doing
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Shell script doesn't run on cron (otherwise it works) kalashari Linux - Software 6 02-17-2012 01:50 PM
[SOLVED] write a shell script so that it can run as service/daemon deostroll Programming 10 03-08-2010 10:03 AM
Script not running properly in cron though runs well if run manually. linuxlover.chaitanya Linux - Newbie 4 01-15-2009 03:31 AM
Cron wont run my shell script shelb Linux - Desktop 4 05-27-2007 09:55 PM
run shell script on cron varunbihani Linux - Newbie 5 07-08-2005 01:50 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration