Is it possible to make commands in crontab run with bash instead of sh? I know you can pass commands to bash with -c, but that's annoying and I never use sh anyway.
Asked
Active
Viewed 9.4k times
109
Fluffy
- 2,047
- 3
- 15
- 18
-
don't really know what you're doing (not familiar with cron) but if you are running a script then adding a shebang to it should work, not sure if I am correct. – Alvin Wong Oct 11 '13 at 17:18
1 Answers
155
You should be able to set the environment variable prior to the cron job running:
SHELL=/bin/bash
5 0 * * * $HOME/bin/daily.job >> $HOME/tmp/out 2>&1
slm
- 363,520
- 117
- 767
- 871
-
3
-
4@Fluffy `.bashrc` is - by definition - for *interactive* shells; so when started via `cron`, `bash` will only read this file if it is an *interactive shell*. – umläute Oct 10 '13 at 16:03
-
1@umläute, ok, so where should I put env vars that should exist in non-interactive non-login shells? – Fluffy Oct 10 '13 at 16:06
-
28I was able to make it read bashrc by adding `BASH_ENV="/root/.bashrc"`. Thanks – Fluffy Oct 10 '13 at 16:12
-
4
-
4Regarding `.bashrc`, `bash -l` looks useful, http://blog.endpoint.com/2015/02/cron-wrapper-keep-your-cron-jobs.html – mpapec May 04 '17 at 05:15
-
1Note that I've observed that `SHELL` requires to be set in each crontab (e.g. the one you modify with `crontab -e`) and setting it only in `/etc/crontab` won't apply globally. – Jaime Hablutzel Apr 02 '20 at 21:10
-
1@JaimeHablutzel that's correct, the system crons and user crontabs (`crontab -e`) are independent from each other – slm Apr 02 '20 at 23:49
-
Will this apply to the entire cron file? Or just the remaining jobs in the crontab lexically? – Sridhar Sarnobat Mar 23 '21 at 22:35
-
I wonder if I can do `SHELL=/bin/sh 2>/tmp/cron_errors.log`. Finding errors in cron jobs is a pain. Am trying now. – Sridhar Sarnobat May 01 '22 at 22:55
-