LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-02-2011, 11:46 AM   #1
raamkum
LQ Newbie
 
Registered: Oct 2011
Posts: 1

Rep: Reputation: Disabled
Cronjob for bash file


Hi,

I have written script that can pick 10 picture randomly from 200 picture and move it a another location and rename the file in series 1 to 10.

This works just fine when I execute the script manually.

Prompt> chmod +x move.sh
prompt> ./move.sh

As per the script in move.sh - will move the files and also send me an email perfectly.

But when I do this in cronjob.

prompt>crontab -e

49 11 * * 3 /bin/chmod +x /kunden/homepages/mylocation/mylocation/htdocs/release/sites/default/files/randompics/move.sh
50 11 * * 3 /bin/bash /kunden/homepages/mylocation/mylocation/htdocs/release/sites/default/files/randompics/move.sh

I couldn't get the file moved but the existing file got deleted as per the code and also sent me an email too...but it didn't do the main job moving random file to given location.

I am not able access the log files also..

Does anyone know how to fix this issue?

Awaitng your response!

Thanks
 
Old 11-02-2011, 12:11 PM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
When you run commands from command line they inherit the environment you got when you logged in. (e.g. /etc/bashrc, /etc/bashprofile, /etc/profile, $HOME/.bashrc, $HOME/.bashprofile, $HOME/.profile and/or others depending on the shell you use.)

When you run commands from cron they have a minimal environment so it is important to insure that the commands have everything they need. You went a step in the right direction by specifying full path to chmod and move.sh but it is likely something WITHIN move.sh itself that is having an issue. For example if it contains the "mv" command but doesn't have the full path to it then it might fail. Also there could be other environmental variables it is expecting. For example if you had at login a variable named "PICPATH" that had the location of your pictures this wouldn't be there in cron so you'd have to include it.

You can fix such issues by doing one of the following:
1) Insure all variables including PATH ($PATH) used in your login environment are set within the move.sh script.
2) "Source" all the files in move.sh that you normally "source" at login (the bashrc and profiles stuff mentioned above). Typically this is less than optimal because it will include terminal settings that aren't approrpiate for a background process run such as cron. (It doesn't necessarilly mean it won't work - it just makes for ugly output in logs.)
3) Use fully qualified paths for ever command and file in move.sh.
4) If you don't have access to modify move.sh itself you could create a "wrapper script" that does option 1 or 2 then runs move.sh. e.g. name a file movewrapper.sh and insert the following:
Code:
$!/bin/bash
export PATH=/bin:/usr/bin:/usr/sbin:/usr/local/bin:/home/myuser/scripts"
export PICPATH=/home/myuser/mypictures
/kunden/homepages/mylocation/mylocation/htdocs/release/sites/default/files/randompics/move.sh
Note that in the above /home/myuser/scripts and /home/myuser/mypictures are made up by me as examples of what you COULD include - they shouldn't actually be included (unless by some fluke they actually exist as such in your environment).

One way to see what is in your login environment is to run "env" or "set". You can then include as much or as little of that in your move.sh or wrapper script.
 
  


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
Bash: bash file names are different than openbox trash file names whatthefunk Programming 4 02-15-2011 01:52 AM
.sh file execute in command window but not in cronjob a9b8c2 Linux - General 5 12-12-2010 09:14 AM
[SOLVED] How write a literal bash command in a bash file? xeon123 Linux - Newbie 6 11-29-2010 12:05 PM
[SOLVED] shell script to import .exp file to oracle from cronjob somupl86 Linux - General 2 11-18-2010 05:57 AM
Bash script for listing FTP usage as the file name of a file created in each share jojothedogboy Programming 1 12-05-2008 03:35 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:30 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