Proper way to run a script using cron?

后端 未结 4 1519
后悔当初
后悔当初 2020-12-19 20:27

When running a script with cron, any executable called inside must have the full path. I discovered this trying to run wondershaper, when many errors showed when it tried to

4条回答
  •  死守一世寂寞
    2020-12-19 20:56

    My recomendation:

    Set all variables in a external file. I use 'process_name.env' file located in /etc/process_name or similar. Imagine you have a backup script. Then you:

    • Create /etc/backup.env and put all environment variables needed for do the "backup" task.
    • Modify your backup script and add this line after Shebang:

      . /etc/backup.env #There is a dot and a space before full path to backup environment.

    IMO this approach is better than declaring variables at CRON definitions because:

    • Easy to maintain. Just edit a file.
    • Easy to switch configuration/centralized configuration:
      • You can have multiple .env for using your script in different situations (for example, consider you have backup locations on your .env, you can pass .env location as an argument and run your cron job daily providing an .env with few locations and weekly with different locations by providing another .env, just a example).
    • You can keep your .env files in a VCS like SVN or Git.
    • Much easy to test your scripts (there is no need to execute it from CRON).

    Regards

提交回复
热议问题