Running Shell Script in the cron, background process

Hi, i was looking for an answer for some trouble im having runing a script in the cron, thing is, that when i run it manually it works just fine. But when cron runs it, it just doenst work. I saw a reply on a similar subject, suggesting that the . .profile worked for you, but im kind of confused and its the reason im looking for your advice. how does the instruction goes in the Shell script??
Is it:

#!/bin/bash
. .profile
command/proccess &

Or

#!/bin/bash
. $HOME/.profile
command/proccess &

Help please, and thanks for your time

Don't rely on $HOME either, put the full path to the profile script you want to use:

#!/bin/bash
. /home/blacksteel1988/.profile
# rest of your script here
1 Like

Unless you give the required command's path (ex. oracle home,java home etc) in .profile file, this file would not help in running cron. So if you want to run a script through cron, give full path in the script for external commands (sqlplus,java etc) and files/logs you want create while running script. In case you want to store a value from a file, which you have already created, we could try as

FILE_VALUE=$(cat temp_file.txt) # stores content of temp_file.txt to variable FILE_VALUE

The above would work if you run the script manually as most of the time we will save the file in the same location where we save the script. If the same needs to run in cron, the shell would not be able to find this .txt file as cron by default to my knowledge will run the script from the user's home directory. Hence the work around would be

FILE_VALUE=$(cat /full/path/to/temp_file.txt)

Similarly its the case of other commands which are not shell bulit-in.

Well i did as Chubler XL suggested but still didn't work.... I'll be more specific and say that the script i want the cronjob to run, runs another script that starts a java proces, and it goes like...

#!/bin/sh
##This script start's another script that contains a java process
##
##starts here...

#1)
. /home/interp/.profile
/home/interp/up.sh &   ------> this script runs the script that start the java process


#Option 2
#. $HOME/.profile
#/home/interp/SH/audit_server_run.sh &  -------> and this is the script that directly starts the java process

exit 0
##End of script

I dunno actually how to make it work, and i appreciate i u guys could help me

So, you are telling, the master script runs when you execute it manually and doesn't run thru cronjob?...
Can you paste the entry of crontab -l
Or are we dealing with something else?
--ahamed

TESTDB: /home/interp> crontab -l
#
##
#Edited by JFMA

#Stop process

32 6 * * * /home/interp/stop.sh  <-----This one works just Fine !!!

#Re start the process

33 6 * * * /home/interp/restart.sh  <----- This is my script, the one i posted above

#33 6 * * * /home/interp/SH/audit_server_run.sh & <----- and this is the script that directly starts the java process 

TESTDB: /home/interp>

Hi, i just wanted to thank you all, and tell you that i make it work..

This is what i did:

#!/bin/bash
#Edited by JFMA
##This is the script
##
##Starts here...

#1)

cd /home/interp/SH

. /home/interp/.profile
./interpreter_server_teller_run.sh &

exit 0

I really dunno exactly why, but it worked!!! after that just put it in the cron.

Probably your script expected to be in a certain directory, using relative paths instead of absolute ones for certain files..

Certainly because you did not only have PATH issues but ENV. by sourcing .profile, you initialized all environmental variables needed for that user...(Perhaps even more... No errors in cron logs?)

Well... not yet... i continue to monitor the process as it has been unstable.... tuesday and friday will be the fire test since those days the process must be running.

I'll let you know.....

By the way... Errors in cron logs? after or before it worked??