1836356 Members
2400 Online
110100 Solutions
New Discussion

Run crontab job

 
SOLVED
Go to solution
hangyu
Regular Advisor

Re: Run crontab job

thx all,

What I hope now is when when run a script by crontab , the env will change to env_cron( see below ) , I don't know why this program is not work , can anyone advise what is wrong ? thx


for i in $(cat /tmp/env_cron)
do
export ${i}
done
Dennis Handly
Acclaimed Contributor

Re: Run crontab job

>the env will change to env_cron (see below), I don't know why this program is not work

You will need to show a few lines of env_cron.
But one issue could be due to embedded blanks if you have: XXX=abc def

You can improve your script by removing the redundant cat:
for i in $(< /tmp/env_cron); do
export ${i}
done
Jov
Honored Contributor

Re: Run crontab job

Hi Hangyu,

Insteading of doing:

for i in $(cat /tmp/env_cron)
do
export ${i}
done

Whey dont you use source the file in '. /tmp/env_cron' for Bourne/Korn Shells or 'source /tmp/env_cron' for C like Shells.

Do this before running a crontab command or include it inside a script.

But like most have stated, your issue is elsewhere and I am still not clear what the problem is as you've not attempted in tracking it down.


Jov
hangyu
Regular Advisor

Re: Run crontab job

thx jov ,

Your suggestion seems very simple ,
I try to write the below script to test it ( I use bash ) , the env (/tmp/check_cron_result
) is still same as that in crontab , can advise what is wrong ? thx



#!/bin/bash
. /tmp/env_cron
env > /tmp/check_cron_result
Dennis Handly
Acclaimed Contributor

Re: Run crontab job

If the environment is the same between your cronjob and outside, then it is something in your .profile. Where is your output of the "set -x" instrumentation (as suggested by Clay) in your .profile?
hangyu
Regular Advisor

Re: Run crontab job

thx Dennis Handly ,

I use bash , so when I use "set -x" , there is no output ,

Now. I am strange that why I use the below method ( in crontab ) , the env is still unchange , but it work fine if I run it manually ?


. /tmp/env_cron
env > /tmp/check_cron_result
Jov
Honored Contributor

Re: Run crontab job

Hi Hangyu,

We want to help but you are confusing the heck out of me. First the script was working from cron, but failed manually. Now are suggesting your script works manually, but fails from cron??

Can you clarify this point some we have a better idea of where you're coming from?

set -x works fro bash as well.


Jov
Dennis Handly
Acclaimed Contributor

Re: Run crontab job

>I use bash, so when I use "set -x", there is no output

Ah, .profile isn't read except by the login shell. So set -x in .profile won't help.

You need to use set -x in your script and see why the output is different. (I guess this was what Clay said way back when.) The -x output goes to stderr.

If this doesn't work, all I can suggest is YOUR bash is broken and use echo statements to track it.

>I use the below method (in crontab), the env is still unchanged, but it work fine if I run it manually?

. /tmp/env_cron
env > /tmp/check_cron_result

This isn't helpful. Please provide the crontab file and script where you do this and what the outputs are. Or provide a diff.

What should happen is all of the ENV VARs you set in your .profile and related files will not be there when run in your crontab.
hangyu
Regular Advisor

Re: Run crontab job

thx replies,

Actually , what the problem I want to fix is the script can run manually but can't run by crontab , so now I want to run the script by crontab sucessfully .

I am not too understand what is the function of "set -x" , so I write a program and put it on the top of the script to change the env , I think it it simplest way , however it is not work , don't you think it is a solution of the problem ? if yes , can advise what is wrong ? thx
Dennis Handly
Acclaimed Contributor

Re: Run crontab job

>is the script can run manually but can't run by crontab, so now I want to run the script by crontab sucessfully.

This is the problem we've been telling you that you may have and you've been saying the exact opposite.

>I am not too understand what is the function of "set -x",

One purpose of the set -x was to track down the opposite problem.

>don't you think it is a solution of the problem?

The purpose of "set -x" is to trace the commands in your script to see where it fails.

So, what are the errors you are getting in your crontab script? Do you redirect the crontab output or do you get a mail message?

As mentioned several times, when you execute a script from crontab, you don't have your environment from .profile, unless you do:
$ . ~/.profile

PATH and env vars are limited. Your umask may be incorrect. Your aliases are not present but you shouldn't be depending on them in any script.