1

I have been trying to set up a cron job to run a bash script every 30 minutes on a Linux-based compute cluster. The problem is that some functions normally available in my shell are not available to the cron job, causing it to crash. I import all my environment variables (including the ~/.bashrc) but no luck, so I am clearly still missing something but have been unsuccessful in identifying what that may be.

As a result, I am trying to find an alternative to cron that mimics the normal login environment to avoid this issue altogether. Does anyone have any suggestions?

Argon
  • 405
  • 1
  • 6
  • 18
  • In don't know of anything cron-like that emulates a normal login, and it's not something I'd expect to exist, because it's not really a good idea. You could try running the script with `bash -l` (or use a shebang like `#!/bin/bash -l`) to make bash run in login-shell mode. But my suspicion is that there's something more obscure that's different, and you're just going to have to keep digging, and find out what it is in order to fix it. – Gordon Davisson Oct 19 '19 at 06:41
  • Can you show the specific error, and maybe the specific code you run? – Uberhumus Oct 19 '19 at 13:31
  • An example cronjob is `*/2 * * * * source /home/myname/.bashrc && cd /work/myname/screen && ./job.sh` with `#!/bin/bash` at the top. In this minimal example, `job.sh` can literally just be as simple as a script with `sbatch sub.job`, which has the job submission info for the job scheduler. Works fine outside cron, but when using cron it complains that several environment variables and functions available on the login node are not available to cron. – Argon Oct 19 '19 at 16:32

1 Answers1

1

Set path at the beginning of your script:

# for example:
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
UtLox
  • 3,744
  • 2
  • 10
  • 13
  • I'm apparently still missing the `module` command, which is a function present on the login node but not by cron despite including the above in my path. I have found the function definition by using `module type`, but I don't know where this is sourced upon login. – Argon Oct 19 '19 at 17:11
  • Okay, I was able to figure it out with your help. I Needed to add the paths you listed, but one of the functions in there didn't load properly for some reason. This caused `module` not to work. By doing `module type`, I got the definition of the `module` function, put it into a new file called `functions.sh` and then sourced `functions.sh` in my cron job. With `~/.bashrc`, the `/usr` paths you listed, and my `functions.sh` all loaded up, my jobs now run appropriately with an environment close, if not identical, to my usual login environment. – Argon Oct 19 '19 at 17:34