Running script in crontab in a specific directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Running script in crontab in a specific directory
# 15  
Old 01-27-2015
A good cron script always starts by running .profile, or some such environment file, to get the same behaviors you had interactively. Writing a .profile that is no-terminal friendly is a bit complex. You need to move all the terminal stuff to the end and test something like 'tty' to see if there is a terminal. Similarly, care needs to be taken if profile is to support both sh and ksh or bash, as sh has fewer tools and different options. e.g., no $(...) or 'set -o vi'. You might write a .profile_cron or the like and source it ( . script_path ) from .profile, so the terminal stuff can be left in .profile but all other stuff is in .profile_cron. Ditto for sh versus ksh/bash: .kshrc and .bash_profile

A few things remain different, like 'ps' options that reflect your terminal, for in cron you have no controlling terminal.

A good script, generally, has execute permissions and starts with a valid #! line as described in man execvp. Without that, it usually goes to sh as stdin.
# 16  
Old 01-27-2015
Thanks, everyone of you for all your replies. I finally figured out how to get around this. Apparently there is sudo - su and sudo bash; these are using different profiles, and the crontab uses the sudo - su profile which is stripped down.

I placed bash in front of the path of the script in the crontab and now all my issues are gone!!

Thanks again for all your suggestions, I certainly learned a ton of new tricks!
# 17  
Old 01-28-2015
You do not need the explicit 'bash' in front of the path of the script with a '#!<full_path_of_bash>' line 1 and execute permissions.
# 18  
Old 01-29-2015
You actually do need bash in front of the script. There is something strange about the environment. I found out there were reports of other strange occurrences with any script not written in sh
# 19  
Old 01-30-2015
Quote:
Originally Posted by newbie2010
Have double checked the typing and it is
Code:
export PATH=$PATH:/local/mnt:/usr/sbin

but I still get "not a valid identifier."
Well, first off: you shouldn't do that. The correct syntax is:

Code:
identifier=content
export identifier

and NOT:

Code:
export identifier=content

I know, this is frequently done, which still doesn't make it correct. Notice, btw., that repeatedly exporting variables is not necessary. The oftenly seen:

Code:
x=ABC
export x
x=DEF
export x
x=GHI
export x

can be written to the same effect as:

Code:
x=ABC
export x
x=DEF
x=GHI

A variable is either exported (inherited by an eventual child process) or not, but once it is it is always exported with the current value.

Another thing is proper quoting. You write:

Code:
export PATH=$PATH:/local/mnt:/usr/sbin

and the mentioned combination of "export" and a declaration notwithstanding: what will happen if $PATH" contains white space? To protect your script from breaking in such a case always protect your variables:

Code:
PATH="${PATH}:/local/mnt:/usr/sbin"


A last thing: scripts should ALWAYS be written in a way so that they are indifferent to the place where they are started from. This means, first and foremost, you should always use absolute pathes to address files:

WRONG:
Code:
inputfile="input.file"
outputfile="output.file"

grep 'something' "$inputfile" | while read line ; do
     do_something "$line"
done > "$outputfile"

This will work or not, depending on which directory the script was started from. Inserting a "cd /some/where" in the first line might help but is bad style (and in the long run dangerous).

CORRECT:
Code:
workdir="/some/where"
inputfile="${workdir}/input.file"
outputfile="${workdir}output.file"

grep 'something' "$inputfile" | while read line ; do
     do_something "$line"
done > "$outputfile"

The variables "$inputfile" and "$outputfile" contain now absolute pathes and the script will work regardless from where it is called from.

One very last thing: DGPickett is correct! If your first line looks like:

Code:
#! /path/to/your/bash

Then the executable "/path/to/your/bash" is used to run the script, not any other executable at all. Even if there is another (version of) bash installed somewhere (like in "/usr/bin" or whereever) it will not be used, but exactly the specified (executable) file. This is the better solution to use because you specify inside the script by which executable it should be run, not outside of it (where it could be changed without changing the script itself)!

I hope this helps.

bakunin

Last edited by bakunin; 01-30-2015 at 05:03 AM..
# 20  
Old 01-30-2015
Hi bakunin,
Actually, the POSIX Standards have required conforming shells to support export commands with the SYNOPSIS forms:
Code:
export variable[=word]
      and
export -p

since 1992.

But, of course, if you're working on a project that is still using a pure Bourne shell, you'd still need to avoid assigning a value to a variable in an export command.
This User Gave Thanks to Don Cragun For This Post:
# 21  
Old 01-30-2015
Quote:
Originally Posted by Don Cragun
Actually, the POSIX Standards have required conforming shells to support export commands with the SYNOPSIS forms [...] since 1992.
Oops. ~blushes~

Quote:
Originally Posted by Don Cragun
But, of course, if you're working on a project that is still using a pure Bourne shell
I have to admit that i do not (any more). I just learned that once and never bothered to change my habits. Thanks for the correction.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

CRONTAB - one python script is not running

Hi! I'm using a RaspberryPi with standard Raspbian. Currently I'm working on some sort of weather station. For now I have three python scripts - one which is updating txt files for website - update1m.py ( it will not be necessary in few next days so I'll delete it) , second one for updating... (4 Replies)
Discussion started by: bartocham
4 Replies

2. Shell Programming and Scripting

Shell scripting-I need a script which should watch a directory for a file with specific directory

I need a script which should watch a directory for a file with specific directory. If it finds a file in directory, it should search for few specific keyword in the file. if the keyword exists, it should trim string from specific column. The file should be moved to another directory and the a... (8 Replies)
Discussion started by: akashdeepak
8 Replies

3. Solaris

Script problem when running on crontab

Hi guys! I created a backup script that works fine when I run manually, but when I put a crontab job to execute it the result are not the expected. (not a time problem). Here is my script: bash-3.00# cat /bk_tool/backup2.sh #!/usr/bin/csh clear set DIR_HOST='SCP08' ... (3 Replies)
Discussion started by: andredemartini
3 Replies

4. UNIX for Dummies Questions & Answers

crontab not running script

Hi All, I am having the below script to be run from crontab, it it doesnt run. 1 * * * * /home/cobr_ext/test.sh > /home/cobr_ext/temp.txt when i run i manally it runs without any issues. Could please help me as to why doesnt it run the script.:( (7 Replies)
Discussion started by: abhi_123
7 Replies

5. UNIX for Advanced & Expert Users

Particular script not running through crontab

Hi, I have created the below script, set -x # Set the Path of Environment file ENV_FILE_DIR=/opt/app/p1trp1c1/sybase/ecdwqdm/xrbid/QDM_Prod/bin LOG_DIR=/opt/app/p1trp1c1/sybase/ecdwqdm/xrbid/QDM_Prod/log export ENV_FILE_DIR export LOG_DIR # Set Audit Environment . ${ENV_FILE_DIR}/QDM.env... (8 Replies)
Discussion started by: yohasini
8 Replies

6. Shell Programming and Scripting

Crontab not running a script

Hi, I posted this in the Solaris forum but I think this one would be more appropriate. I created a script starting with the following lines: #!/usr/bin/ksh flag=n export flag typeset -i quant=0 (...) When running it I'm getting the following 2 errors: /tmp/tstscript/testfail.ksh:... (9 Replies)
Discussion started by: Cvg
9 Replies

7. Shell Programming and Scripting

Running SQLPLUS Script in CRONTAB

Hi, Can someone please help me here with this one. This is my script: # more tosh.sh #!/usr/bin/ksh clear . /home/oracle/.profile echo "Good morning, world." export ORACLE_HOME=/u01/app/oracle/product/9.0.1 export PATH=$ORACLE_HOME/bin:/usr/local/bin export ORACLE_SID=xxxx ... (11 Replies)
Discussion started by: santoshpayal
11 Replies

8. Shell Programming and Scripting

Problem with crontab running a script

I am trying to use the CRON utility in Fedora 11 & CentOS... I intend to run a script which pops up a warning message every hour and i made the following entry using "CRONTAB -e " * * * * * sh /bin/myscript.sh But this does not seem to be running. Another thing to note is that,... (4 Replies)
Discussion started by: Vabiosis
4 Replies

9. UNIX for Dummies Questions & Answers

Getting error when running script through crontab

Hi all, I wrote small script for Solaris and when I am running it through command prompt its ok, but when I trying to run it using crontab, i am getting error like: ld.so.1: dbloader: fatal: libACE.so: open failed: No such file or directory /tmp/file.sh: line 5: 8304 Killed ... (4 Replies)
Discussion started by: nypreH
4 Replies

10. Shell Programming and Scripting

issue with running script with crontab

I am facing a strange issue while running a script(eg A) from the crontab entry the script calls one more script(eg B) within it now when i run the script A manually(with nohup) it also executes the script B (embedded inside it) as expected. but when i run the script A from the crontab entry... (7 Replies)
Discussion started by: mad_man12
7 Replies
Login or Register to Ask a Question