0

I have tried to update my code base on a daily basis using the following crontab command. But the first step itself not working when I tested it.

27 15 * * * . cd /home/covuser/user/ && . /home/covuser/user/conf.sh
40 17 * * * . cd /home/covuser/user && . /home/covuser/user/checkout.sh

conf.sh contains the environment variable necessary to make the code checkout. checkout.sh file does the checkout process.

The first line is supposed to set the environment variable required. But it is not happening.

What mistake have done in this? Thanks in advance.

  • 1
    What are the periods supposed to be doing? – steeldriver Jun 14 '18 at 11:02
  • I suspect that both scripts are running, but in different context, hence the environment variables in the first script aren't available when the second script is running – Yaron Jun 14 '18 at 11:04
  • the periods are supposed to make the process run in a same shell instead of running it in a child shell. I have tried this to make sure it enters the preferred directory and then sets the environment variable. – user2670573 Jun 14 '18 at 11:12
  • @Yaron . If that happened, atleast I should be able to see the environment variable set. But , that itself is not happening. Sorry i'm getting confused – user2670573 Jun 14 '18 at 11:14
  • 1
    @user2670573 please edit your question to describe what the scripts do and how they are supposed to work together – steeldriver Jun 14 '18 at 11:26
1

Each line in your crontab represents a separate job, which will be scheduled and executed independently.

That means in your case especially that they run in different shell sessions, so that sourcing a script file to set some environment variables in one job will not affect the environment in which another job will run later.

You'd have to change your process and put run both scripts within the same shell session and therefore most easily just in the same cron job.

If you need a delay between the runs, you might achieve that e.g. with sleep instead of scheduling a different job with a fixed later time.


Oh, and of course, as pointed out in some of the other comments and answers, you can only source scripts with . to make them run in the current shell environment instead of a subshell. However, cd is not a script but a built-in shell command, which always runs in the current shell environment anyway, so writing . cd somehwere is an error too.

0

I believe the problem is with the . preceding your cd.

. (alias fro source) lets you source a file. This does not work for shell builtins, like cd, as they are not executable files which can be sourced.

As you concatenate the two commands with an AND - &&, the shell skips any command following one which fails, as they will not change the resulting outcome value (false && true == false). You could replace the && with a ; if you wish for another behavior.

This should work :

27 15 * * * cd /home/covuser/user/ && . /home/covuser/user/conf.sh
40 17 * * * cd /home/covuser/user && . /home/covuser/user/checkout.sh

Update : If the scripts are dependent, (which they are i think ?), run everything in one single session. (A new bash session will be opened to execute each cron entry)

27 15 * * * cd /home/covuser/user/ && . /home/covuser/user/conf.sh && . /home/covuser/user/checkout.sh
  • 1
    and +1 to Bytes answer, he saw the bigger issue : if the two scripts are dependent, they should be run in the same shell session – Vince Jun 14 '18 at 11:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.