Script returns errors if not logged id

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
taouk
Level 1
Level 1
Posts: 6
Joined: Mon Nov 20, 2017 4:54 am

Script returns errors if not logged id

Post by taouk »

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?
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.
rene
Level 20
Level 20
Posts: 12240
Joined: Sun Mar 27, 2016 6:58 pm

Re: Script returns errors if not logged id

Post by rene »

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: 6
Joined: Mon Nov 20, 2017 4:54 am

Re: Script returns errors if not logged id

Post by taouk »

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 20
Level 20
Posts: 12240
Joined: Sun Mar 27, 2016 6:58 pm

Re: Script returns errors if not logged id

Post by rene »

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: 1432
Joined: Mon Jun 22, 2009 3:26 am
Location: Philippines

Re: Script returns errors if not logged id

Post by Pilosopong Tasyo »

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.
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: 6
Joined: Mon Nov 20, 2017 4:54 am

Re: Script returns errors if not logged id

Post by taouk »

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: 6
Joined: Mon Nov 20, 2017 4:54 am

Re: Script returns errors if not logged id

Post by taouk »

Thank you for helping. It is Solved
Locked

Return to “Scripts & Bash”