Script returns errors if not logged id

Forum rules
Before you post please read how to get help
taouk
Level 1
Level 1
Posts: 4
Joined: Mon Nov 20, 2017 4:54 am

Script returns errors if not logged id

Postby taouk » Mon Nov 20, 2017 5:07 am

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?

rene
Level 6
Level 6
Posts: 1371
Joined: Sun Mar 27, 2016 6:58 pm

Re: Script returns errors if not logged id

Postby rene » Mon Nov 20, 2017 6:55 am

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.

taouk
Level 1
Level 1
Posts: 4
Joined: Mon Nov 20, 2017 4:54 am

Re: Script returns errors if not logged id

Postby taouk » Mon Nov 20, 2017 8:58 am

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.

rene
Level 6
Level 6
Posts: 1371
Joined: Sun Mar 27, 2016 6:58 pm

Re: Script returns errors if not logged id

Postby rene » Mon Nov 20, 2017 9:26 am

taouk wrote:7) If cron the same script and i am logged in, everything is ok.

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.

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.)

User avatar
Pilosopong Tasyo
Level 6
Level 6
Posts: 1424
Joined: Mon Jun 22, 2009 3:26 am
Location: Philippines

Re: Script returns errors if not logged id

Postby Pilosopong Tasyo » Mon Nov 20, 2017 9:51 am

taouk wrote:2) The script is called a minute after the pc starts without logging in

According to your cron table, 1.sh will get called everyday (or every weekday, if the last entry was supposed to be 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.
Image
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].

taouk
Level 1
Level 1
Posts: 4
Joined: Mon Nov 20, 2017 4:54 am

Re: Script returns errors if not logged id

Postby taouk » Tue Nov 21, 2017 7:37 am

Pilosopong Tasyo wrote:
taouk wrote:2) The script is called a minute after the pc starts without logging in

According to your cron table, 1.sh will get called everyday (or every weekday, if the last entry was supposed to be 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.

-------------------------------------------------------------------------------------------------------------------------------------------
Thank you very much. I did have a mistake in crontab (more arguments).

taouk
Level 1
Level 1
Posts: 4
Joined: Mon Nov 20, 2017 4:54 am

Re: Script returns errors if not logged id

Postby taouk » Tue Nov 21, 2017 7:43 am

Thank you for helping. It is Solved


Return to “Scripts & Bash”