Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to simulate the environment cron executes a script with?

People also ask

Can you use environment variables in crontab?

You can define environment variables in the crontab itself when running crontab -e from the command line. This feature is only available to certain implementations of cron. Ubuntu and Debian currently use vixie-cron which allows these to be declared in the crontab file (also GNU mcron).

How do I test a cron script?

How to test a Cron Job? Open the Corntab – Its an online tool that will help you to Check the Cron time. You can enter the cron time and it will tell you when this cron will trigger. Note down the time and verify if its correct one.


Add this to your crontab (temporarily):

* * * * * env > ~/cronenv

After it runs, do this:

env - `cat ~/cronenv` /bin/sh

This assumes that your cron runs /bin/sh, which is the default regardless of the user's default shell.

Footnote: if env contains more advanced config, eg PS1=$(__git_ps1 " (%s)")$, it will error cryptically env: ": No such file or directory.


Cron provides only this environment by default :

  • HOME user's home directory
  • LOGNAME user's login
  • PATH=/usr/bin:/usr/sbin
  • SHELL=/usr/bin/sh

If you need more you can source a script where you define your environment before the scheduling table in the crontab.


Couple of approaches:

  1. Export cron env and source it:

    Add

    * * * * * env > ~/cronenv
    

    to your crontab, let it run once, turn it back off, then run

    env - `cat ~/cronenv` /bin/sh
    

    And you are now inside a sh session which has cron's environment

  2. Bring your environment to cron

    You could skip above exercise and just do a . ~/.profile in front of your cron job, e.g.

    * * * * * . ~/.profile; your_command
    
  3. Use screen

    Above two solutions still fail in that they provide an environment connected to a running X session, with access to dbus etc. For example, on Ubuntu, nmcli (Network Manager) will work in above two approaches, but still fail in cron.

    * * * * * /usr/bin/screen -dm
    

    Add above line to cron, let it run once, turn it back off. Connect to your screen session (screen -r). If you are checking the screen session has been created (with ps) be aware that they are sometimes in capitals (e.g. ps | grep SCREEN)

    Now even nmcli and similar will fail.


You can run:

env - your_command arguments

This will run your_command with empty environment.


Depending on the shell of the account

sudo su
env -i /bin/sh

or

sudo su
env -i /bin/bash --noprofile --norc

From http://matthew.mceachen.us/blog/howto-simulate-the-cron-environment-1018.html