1833780 Members
2074 Online
110063 Solutions
New Discussion

Run crontab job problem

 
SOLVED
Go to solution
ust3
Regular Advisor

Run crontab job problem

I post the simliar question previously , but still not have solution , now I have a stupid solution for it , but do not know whether it is OK , actucally we use linux system , the below is the question I posted.


http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=1127866


The stupid solution is :
because the job is unable to run by crontab job and is normal to run manually , is it OK to set before run this crontab job , it su to a specific user "cron_usr" ( this cron_usr will be dedicated as a login a/c to run crontab job) , and I will modify the ~cron_usr/.bash_profile , so that when this user is login , it will run the crontab job automatically , as a result I can make sure this crontab job run the normal user profile , please advise is it OK to do that ? thx
30 REPLIES 30
Dennis Handly
Acclaimed Contributor
Solution

Re: Run crontab job problem

>I post the similar question previously

(It seems it was hangyu's question?)

>because the job is unable to run by crontab job

Do you know exactly why?

If you want to run it under cron, you must source your .profile, either before you run your script or in it.

You shouldn't have to do something as complex as you proposed.
ust3
Regular Advisor

Re: Run crontab job problem

It seems it was hangyu's question
> Yes , I lost the password.

Do you know exactly why?
> Still not know the reason , but I guess it should be the difference of .profile .


You shouldn't have to do something as complex as you proposed.
> do you think the solution that I post is possible ? thx
ust3
Regular Advisor

Re: Run crontab job problem

thx reply ,


I tried this method , I guess the theroy is OK , but not work when I try to use "su - cron_usr" in the crontab job , what I want is just to run a crontab job , this crontab job will do the login ( userid is cron_usr ) , this user will do the script that I want when login , can advise how to do this ? thx
ust3
Regular Advisor

Re: Run crontab job problem

thx reply ,

I tried to rlogin stead su , but still not work , how to run a crontab job that will do the user login ? thx
ust3
Regular Advisor

Re: Run crontab job problem

May be my question is not too clear ,what I am testing is :

I tried to write a scrip /tmp/script1


#vi /tmp/script1
rlogn localhost -l cron_usr

#vi ~cron_usr/.profile
my_script

if I run this script manually , it is OK , but if I run it by crontab job , it is not work , so I think run manully and by crontab is different , can advise how can I make it work ?

thx
V. Nyga
Honored Contributor

Re: Run crontab job problem

Hi,

why don't you make a copy of your command - use one for running manually, in the other revers all changings you've made and run it with cron, as it worked before?

By the way - which linux are you using?

Volkmar
*** Say 'Thanks' with Kudos ***
ust3
Regular Advisor

Re: Run crontab job problem

thx reply ,

I use Redhat Ent. 3
ust3
Regular Advisor

Re: Run crontab job problem

whether I am not clear state what I want to do ? or it is impossible to do what I want ?

thx
ust3
Regular Advisor

Re: Run crontab job problem

thx reply,

why don't you make a copy of your command - use one for running manually, in the other revers all changings you've made and run it with cron, as it worked before?
> sorry , I am not too understand what is that mean ,
but for simply , I just want to setup a crontab job , when run this job , it will login the system ( just like a telnet , ssh etc ) , is it possible ? thx
V. Nyga
Honored Contributor

Re: Run crontab job problem

Hi again,

check your previous thread, where the cron job worked!

For Linux you should ask here:
http://forums1.itrc.hp.com/service/forums/familyhome.do?familyId=118

HTH
Volkmar
*** Say 'Thanks' with Kudos ***
Yogeeraj_1
Honored Contributor

Re: Run crontab job problem

hi,

did you try this?

#*******************************************************************************
# min|hour |day |month|day |script
# | |of mo| |of wk|
#----|-----|-----|-----|-----|--------------------------------------------------
#*******************************************************************************
00 21 * * * echo "/home/yogeeraj/myscript.sh; exit"|su - cron_usr 1>/home/yogeeraj/logfiles/output_script.crn 2>/home/yogeeraj/logfiles/error_script.crn
#*******************************************************************************
# END OF TABLE day0->Sunday day6->Saturday
#*******************************************************************************

Kind regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Yogeeraj_1
Honored Contributor

Re: Run crontab job problem

or maybe:

00 21 * * * echo "/home/cron_usr/myscript.sh; exit"|su - cron_usr 1>/home/cron_usr/logfiles/output_script.crn 2>/home/cron_usr/logfiles/error_script.crn


kind regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Dennis Handly
Acclaimed Contributor

Re: Run crontab job problem

>Yes, I lost the password.

You can't recover the password because you don't know the old email alias or it no longer exists?

>but I guess it should be the difference of .profile.

Then source that .profile in your script.

>do you think the solution that I post is possible?

As I said, why do something that complex.

>"su - cron_usr" in the crontab job

This will ask for a password if not root. You can't do that in a cronjob.

>this crontab job will do the login (userid is cron_usr), this user will do the script that I want when login, can advise how to do this?

You can set up a crontab for cron_usr. Let that do the work.

>how to run a crontab job that will do the user login?

You can't unless root.

#vi /tmp/script1
rlogin localhost -l cron_usr

>#vi ~cron_usr/.profile
my_script

You don't want to put anything executable like that in .profile.

>can advise how can I make it work?

You create a crontab entry for cron_usr to execute ~/my_script.

In my_script you source .profile:
. ~/.profile

>Volkmar: why don't you make a copy of your command - use one for running manually, ...

There is no reason you can't use one script for both.

>I just want to setup a crontab job, when run this job, it will login the system (just like a telnet

There is no need to login to the system like telnet, just "login" like crontab.

>yogeeraj: echo "/home/cron_usr/myscript.sh; exit"|su - cron_usr

It would be much easier to fiddle with the script than to make the crontab entry so complex.

Also, root can use:
su - cron_usr -c "/home/cron_usr/myscript.sh"
Mario_88
Advisor

Re: Run crontab job problem

I haven´t read all of thie but one advice. Use allways the full path to the binaries when using crontab, also if you need any environment variables set them in the script.
ust3
Regular Advisor

Re: Run crontab job problem

thx replies ,

when run echo "/home/yogeeraj/myscript.sh; exit"|su - cron_usr , it pop "standard in must be a tty" . thx
Yogeeraj_1
Honored Contributor

Re: Run crontab job problem

hi again,

the following should on the same line:
00 21 * * * echo "/home/cron_usr/myscript.sh; exit"|su - cron_usr 1>/home/cron_usr/logfiles/output_script.crn 2>/home/cron_usr/logfiles/error_script.crn


so that the standard output and error are redirected to the desired files.

kind regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Dennis Handly
Acclaimed Contributor

Re: Run crontab job problem

when run echo "/home/yogeeraj/myscript.sh; exit"|su - cron_usr , it pop "standard in must be a tty"

I believe we mentioned in your previous thread that you need to add "set -x" to your profile so you can find what command is doing that.

Clay mentioned: the fact that stdin, stdout, and/or stderr are terminal devices ...

Typically this is a common mistake in .profile where the tty commands aren't guarded by checks:
# Set up the terminal:
if [ "${-%%*i*}" != "$-" ]; then
/bin/stty erase "^H" kill "^X" intr "^C" eof "^D" susp "^z"
fi

Similarly for ttytype, tset or resize.
A. Clay Stephenson
Acclaimed Contributor

Re: Run crontab job problem

... and the much smarter way is to not do an "su - mickey" but simply an "su mickey ..." so that the user's .profile is not sourced. What you do is create a file something like
/usr/local/bin/user_mickey_env.sh and then both your cron'ed script AND mickey's .profile source this file using the "." (dot)operator "./usr/local/bin/user_mickey_env.sh" and then the environment variables including PATH are set in this file. This way, you avoid the potential trapfalls where .profile expects an interactive environment and commands hang or fail. This sourced file must not contain an exit or return statement because the effect will be to exit the foreground process (the current shell).
If it ain't broke, I can fix that.
Dennis Handly
Acclaimed Contributor

Re: Run crontab job problem

>Clay: the much smarter way is to not do an "su - mickey"

A smarter way to do it is to not do su at all. Just add a crontab entry for mickey.

>What you do is create a file something like
/usr/local/bin/user_mickey_env.sh

It seems like the right thing to do is fix your .profile. That way you don't come back next year with the same problem on a different script.

(I'm not sure why you would want to put mickey specific stuff in /usr/local/, rather than in ~mickey/.)
A. Clay Stephenson
Acclaimed Contributor

Re: Run crontab job problem

... unless this is a sequence of operations that must be run in strict order as a number of different users then a cron script run by root su'ing makes perfect sense.
If it ain't broke, I can fix that.
Dennis Handly
Acclaimed Contributor

Re: Run crontab job problem

>Clay: a cron script run by root su'ing makes perfect sense.

I suppose you are correct if this user is really a dummy user for something like Oracle or SAP, where it is done on behalf of the system.
ust3
Regular Advisor

Re: Run crontab job problem

thx replies,

do you mean add "su - cron_usr" to
/usr/local/bin/cron_usr_env.sh , I tried this method , but it is OK to run at shell ( manual run ) but not work to run by crontab job ,
thx
ust3
Regular Advisor

Re: Run crontab job problem

thx reply ,

I got many suggestions but I still not fix the problem yet ,
as my previous message , is it possible to run a batch job (eg. /tmp/script1) that will do the login ( eg. ssh , telnet , rlogin )

for example :
#vi /tmp/script1
su - cron_usr

#vi ~cron_usr/.profile
my_script
when run the crontab job ( /tmp/script1) , it will su to cron_usr , then it will run my_script .

thx in advance.
Dennis Handly
Acclaimed Contributor

Re: Run crontab job problem

>is it possible to run a batch job (eg. /tmp/script1) that will do the login (eg. ssh , telnet, rlogin)

You can do it as root.

>for example :
/tmp/script1:
su - cron_usr

~cron_usr/.profile:
my_script
when run the crontab job (/tmp/script1), it will su to cron_usr, then it will run my_script .

I did all of this and except for the error messages from /etc/profile, it worked.

The crontab stderr had:
ttytype: couldn't open /dev/tty for reading
stty: : Not a typewriter

The crontab stdout had copyrights, /etc/motd
and "logout" upon the EOF in .profile.

Basically cron does NOT want/expect you to do "su -".

This is an awfully long way to go when you can just fix the script to source what it needs out of a modified .profile.