rajendraone
asked on
user cron shell ecript
Hello,
I have written a small script (script.sh) as:
#!/bin/sh
if [ 1=1 ] ; then
touch donefile
fi
I have written this as a user and can successfully run it. I put it on my cron job.
10 10 * * * /home/user/script.sh
But it wont works. Cron jobs are running. No errors. But it doesnot create file donefile when cron job executes. This i am doing as user. Not root. I have restarted the crond ( ofcourse, as a root). But not working. I can run this script manually but not with cron jobs. I heard there's something needed about environment variables. So i even did as:
. $HOME/.bash_profile
And it didnt worked.
I even gave the path as
PATH=$PATH:/usr/bin
But it also didnt work. (/usr/bin is where [ and ] are)
I think cron job is not recognizing the test ([ and ]) commands.
Please give me some idea of doing this.
Regds,
Rajendra.
I have written a small script (script.sh) as:
#!/bin/sh
if [ 1=1 ] ; then
touch donefile
fi
I have written this as a user and can successfully run it. I put it on my cron job.
10 10 * * * /home/user/script.sh
But it wont works. Cron jobs are running. No errors. But it doesnot create file donefile when cron job executes. This i am doing as user. Not root. I have restarted the crond ( ofcourse, as a root). But not working. I can run this script manually but not with cron jobs. I heard there's something needed about environment variables. So i even did as:
. $HOME/.bash_profile
And it didnt worked.
I even gave the path as
PATH=$PATH:/usr/bin
But it also didnt work. (/usr/bin is where [ and ] are)
I think cron job is not recognizing the test ([ and ]) commands.
Please give me some idea of doing this.
Regds,
Rajendra.
The problem is probably more that it can't find the path to donefile. Put in the fully qualified path, and I suspect that your script will work better :)
if [ 1=1 ] ; then
cd /home/user/
touch donefile
fi
or
if [ 1=1 ] ; then
touch /home/user/donefile
fi
cd /home/user/
touch donefile
fi
or
if [ 1=1 ] ; then
touch /home/user/donefile
fi
ASKER
No man! That's not the problem. It works even if i simply do as:
---------------------
#!/bin/sh
touch donefile
----------------------
This works when script is run manually. Also works when put in the cron job. .i.e. donefile is created (in the dircetory where this script is) in both the cases.
But won't work if i do as:
------------------------
#!/bin/sh
if [ 1=1 ] ; then
touch donefile
fi
--------------------------
This works when i execute the script. But doesn't when it is put in cron job. Fom log file i find cron jobs are executed. I have added to my cron job not on that of root. I think i have clearly stated these things in my question. I think there is a problem with the environment variables, and i dunno what and how to associate these in my cron job.
And, yes i didnt forget to restart the crond service.
---------------------
#!/bin/sh
touch donefile
----------------------
This works when script is run manually. Also works when put in the cron job. .i.e. donefile is created (in the dircetory where this script is) in both the cases.
But won't work if i do as:
------------------------
#!/bin/sh
if [ 1=1 ] ; then
touch donefile
fi
--------------------------
This works when i execute the script. But doesn't when it is put in cron job. Fom log file i find cron jobs are executed. I have added to my cron job not on that of root. I think i have clearly stated these things in my question. I think there is a problem with the environment variables, and i dunno what and how to associate these in my cron job.
And, yes i didnt forget to restart the crond service.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
No ..its the same thing. I am repeatedly saying that thge things work cent percent when run manually but not in the cron job. So there is no point on changing the syntax. Please try it on your machine and tell me if it works.
create user : user
log in to user.
Crate a script file. script.sh
-------------script.sh---- ----------
if [ 1 -eq 1 ] ; then
touch donefile
fi
-------------------------- ---------- --
chmod it to : 755
run the file ./script.sh. It should create a file donefile
Now do as:
crontab -e
add the following line:
2 12 * * * /home/user/script.sh
change the time as per your convience.
log on to root, restart cron job. And log out to login to user again.
After the cron job run. The file will not be created. However, if you rmove the if statement from the script and just do touch file. The cron job creates the file.
So: it can be concluded that, the script works fine if it is run manually but not from the cron job. However, the second case showed that the cron job is working fine for this user. But NOT ONLY SUPPORTINF THE SCRIPT WITH IF.
I doubt there i need to give or include some kind of path variables or someother environment variables.
This is my question from beginning. I think i must have clearly explained this time.
Hope some of you must have faced similar problem.
Regds,
Rajendra.
create user : user
log in to user.
Crate a script file. script.sh
-------------script.sh----
if [ 1 -eq 1 ] ; then
touch donefile
fi
--------------------------
chmod it to : 755
run the file ./script.sh. It should create a file donefile
Now do as:
crontab -e
add the following line:
2 12 * * * /home/user/script.sh
change the time as per your convience.
log on to root, restart cron job. And log out to login to user again.
After the cron job run. The file will not be created. However, if you rmove the if statement from the script and just do touch file. The cron job creates the file.
So: it can be concluded that, the script works fine if it is run manually but not from the cron job. However, the second case showed that the cron job is working fine for this user. But NOT ONLY SUPPORTINF THE SCRIPT WITH IF.
I doubt there i need to give or include some kind of path variables or someother environment variables.
This is my question from beginning. I think i must have clearly explained this time.
Hope some of you must have faced similar problem.
Regds,
Rajendra.
ASKER
Okay..I think I have narrowed down the problem.
IT WORKS AS EXPECTED WHEN SCRIPT IS CREATED AT USERS HOME DIRECTORY.
i.e. /home/user/script.sh
but not when /home/user/mydir/script.sh
Do i need to import bash profile and bashrc or someother thing in the script?
The permission of mydir and script.sh is 777.
Sorry, I forget to mention this thing in above comment. Please try the above within somedirectory in users home directory.
Regds,
Rajendra.
IT WORKS AS EXPECTED WHEN SCRIPT IS CREATED AT USERS HOME DIRECTORY.
i.e. /home/user/script.sh
but not when /home/user/mydir/script.sh
Do i need to import bash profile and bashrc or someother thing in the script?
The permission of mydir and script.sh is 777.
Sorry, I forget to mention this thing in above comment. Please try the above within somedirectory in users home directory.
Regds,
Rajendra.
ASKER
yeah..finally i did it myself.
You should include:
. $HOME/.bash_profile
. $HOME/.bashrc
You should include:
. $HOME/.bash_profile
. $HOME/.bashrc
ASKER
wesley_chen ...you are eligible for the points.
Thanks fpr following my questions and giving a quck response.
Rajendra.
Thanks fpr following my questions and giving a quck response.
Rajendra.
cron job doesn't source the environment variables from user's .bashrc or .profile because the cron daemon is started by root.
So you need to source your .bashrc in your cron script if you need it.
However, your sample script is very simple, so I don't see the need to source your .bashrc here.
Usually, for platform independence, I will do
--------------------
#!/bin/sh
if [ 1=1 ] ; then
/bin/touch ./donefile
fi
-------------------
But if your script are going to need some environment variables setting such as $PATH, $LD_LIBRARY_PATH, then I
recommend that set those variables in your script instead of source your .bashrc.
There are certain situation that this way will cause the trouble since the script call another script in it and the environment
variable too long to fit into the limitation and being truncated.
Regards,
Wesly
So you need to source your .bashrc in your cron script if you need it.
However, your sample script is very simple, so I don't see the need to source your .bashrc here.
Usually, for platform independence, I will do
--------------------
#!/bin/sh
if [ 1=1 ] ; then
/bin/touch ./donefile
fi
-------------------
But if your script are going to need some environment variables setting such as $PATH, $LD_LIBRARY_PATH, then I
recommend that set those variables in your script instead of source your .bashrc.
There are certain situation that this way will cause the trouble since the script call another script in it and the environment
variable too long to fit into the limitation and being truncated.
Regards,
Wesly