I want to execute a bash script without being logged in. I call the script using crontab. Even the simplest script returns error:"/bin/sh: 1: 1-5: not found", although the first line is
"#!/bin/bash". The same script works fine if i am logged in. Is there any suggestions?
Script returns errors if not logged id
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Topics in this forum are automatically closed 6 months after creation.
Script returns errors if not logged id
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Re: Script returns errors if not logged id
Cron jobs get an only minimal environment, including PATH. If you are attempting to execute something from e.g. /sbin you should do do using the full path. If not already helped post at least some detail; crontab line and script.
Re: Script returns errors if not logged id
1) I edit crontab as root
2) The script is called a minute after the pc starts without logging in
3)This is the line in crontab
19 13 * * * 1-5 /usr/billy_scripts/1.sh>>/usr/billy_scripts/mycronlog.log 2>&1
4)These are file permissions in folder /usr/billy_scripts/
drwxrwxr-x 3 root root 4096 Nov 20 13:16 .
drwxr-xr-x 11 root root 4096 Nov 8 14:21 ..
-rwxr-xr-x 1 root root 59 Nov 20 12:18 1.sh
-rwxrwxrwx 1 root root 27 Nov 20 13:19 mycronlog.log
drwxrwxrwx 2 root root 4096 Nov 8 14:56 SERVER_MOUNT_POINT
-rw-rw-rw- 1 root root 1 Nov 20 12:21 test.txt
5)This is the 1.sh script that i call for execution
#!/bin/bash
echo $(date)
6) This is the error message that is written in /usr/billy_scripts/mycronlog.log
/bin/sh: 1: 1-5: not found (although i ask for bash it something for sh)
7) If cron the same script and i am logged in, everything is ok.
2) The script is called a minute after the pc starts without logging in
3)This is the line in crontab
19 13 * * * 1-5 /usr/billy_scripts/1.sh>>/usr/billy_scripts/mycronlog.log 2>&1
4)These are file permissions in folder /usr/billy_scripts/
drwxrwxr-x 3 root root 4096 Nov 20 13:16 .
drwxr-xr-x 11 root root 4096 Nov 8 14:21 ..
-rwxr-xr-x 1 root root 59 Nov 20 12:18 1.sh
-rwxrwxrwx 1 root root 27 Nov 20 13:19 mycronlog.log
drwxrwxrwx 2 root root 4096 Nov 8 14:56 SERVER_MOUNT_POINT
-rw-rw-rw- 1 root root 1 Nov 20 12:21 test.txt
5)This is the 1.sh script that i call for execution
#!/bin/bash
echo $(date)
6) This is the error message that is written in /usr/billy_scripts/mycronlog.log
/bin/sh: 1: 1-5: not found (although i ask for bash it something for sh)
7) If cron the same script and i am logged in, everything is ok.
Re: Script returns errors if not logged id
That sentence doesn't parse ("if cron the same script") but there's an issue in your crontab line. I expect the 1-5 was supposed to be the day-of-week field? Right now you have a "*" too many and the "1-5" becomes the command.taouk wrote:7) If cron the same script and i am logged in, everything is ok.
Note also that /bin/sh is the shell through which the crontab line is executed by cron. You use it to relay to a /bin/bash script; it's only the contents of that script that executes with /bin/bash (and in this specific case you don't need it: might as well use
date
or /bin/date
directly from the crontab line -- but I expect it's a minimal example only.)- Pilosopong Tasyo
- Level 6
- Posts: 1432
- Joined: Mon Jun 22, 2009 3:26 am
- Location: Philippines
Re: Script returns errors if not logged id
According to your cron table, 1.sh will get called everyday (or every weekday, if the last entry was supposed to betaouk wrote:2) The script is called a minute after the pc starts without logging in
1-5
instead of *
-- see what the user above me wrote about correct crontab syntax) at 13:19 (1:19pm). If you want the script to run a minute after boot, use:@reboot /bin/sleep 60s ; /usr/billy...
or
@reboot /usr/billy...
and add
sleep 60s
in the script after the shebang directive.o Give a man a fish and he will eat for a day. Teach him how to fish and he will eat for a lifetime!
o If an issue has been fixed, please edit your first post and add the word [SOLVED].
o If an issue has been fixed, please edit your first post and add the word [SOLVED].
Re: Script returns errors if not logged id
-------------------------------------------------------------------------------------------------------------------------------------------Pilosopong Tasyo wrote:According to your cron table, 1.sh will get called everyday (or every weekday, if the last entry was supposed to betaouk wrote:2) The script is called a minute after the pc starts without logging in1-5
instead of*
-- see what the user above me wrote about correct crontab syntax) at 13:19 (1:19pm). If you want the script to run a minute after boot, use:
@reboot /bin/sleep 60s ; /usr/billy...
or
@reboot /usr/billy...
and addsleep 60s
in the script after the shebang directive.
Thank you very much. I did have a mistake in crontab (more arguments).
Re: Script returns errors if not logged id
Thank you for helping. It is Solved