1

I am new to linux and the script below is just an example of my issue:

I have a script which works as expected when I execute it however when I set it to run via crontab it doesn't work as expected because it doesn't read the file content into the variable.

I have a file 'test.txt' which has 'abc' in it. My script puts the text into a variable 'var' and then I echo it out to a log file:

var=$(</home/pi/MyScripts/test.txt)
echo "$var" >/home/pi/MyScripts/log.log

This works perfectly fine when I execute it and it echo's into the log file but not when I set it via crontab:

* * * * * /home/pi/MyScripts/test.sh

The cron job runs, and it sent me the following error message:

/bin/sh: 1: /home/pi/MyScripts/test.sh: Permission denied.

But I have given it 777 permissions:

-rwxrwxrwx 1 pi pi   25 Jun 10 15:31 test.txt
-rwxrwxrwx 1 pi pi   77 Jun 10 15:34 test.sh

Any ideas?

9
  • 5
    Make sure to put the shebang on top. Like #!/bin/bash. Commented Jun 10, 2014 at 16:43
  • How often do you want it to execute? Every minute? Commented Jun 10, 2014 at 16:43
  • If it couldn't open the file you would get an error message emailed to you. Commented Jun 10, 2014 at 16:44
  • 2
    Whatever other problems you may have, 0777 is always wrong, and a security risk. Sane permissions for this would be e.g 0755. Commented Jun 10, 2014 at 17:04
  • 1
    @JakeGould You need +x on all directories leading up to execute a script, even if the script itself is 777 and you use absolute path. Also, the error is in executing /home/pi/MyScripts/test.sh. Not executing line 1 of this file. Commented Jun 10, 2014 at 17:14

3 Answers 3

2

This happens when you run the script with a different shell. It's especially relevant for systems where /bin/sh is dash:

$ cat myscript 
echo "$(< file)"

$ bash myscript
hello world

$ sh myscript

$ 

To fix it, add #!/bin/bash as the first line in your script.

Sign up to request clarification or add additional context in comments.

Comments

1

Others have provided answers, but I will give you a big clue from your error message; emphasis mine:

/bin/sh: 1: /home/pi/MyScripts/test.sh: Permission denied.

Note how the cron job was trying to use /bin/sh to run the script. That’s solved by always indicating which shell you want to use at the top of your script like this.

#!/bin/bash
var=$(</home/pi/MyScripts/test.txt)
echo "$var" >/home/pi/MyScripts/log.log

If your script is using bash, then you must explicitly set /bin/bash in some way.

Also, regarding permissions you say this:

But I have given it 777 permissions:

First, 777 permissions is a massive security risk. If you do that it means that anyone or anything on the system can read, write & execute the file. Don’t do that. In the case of a cron job the only entity that needs 7 permissions on a file is the owner of the crontab running that file.

Meaning if this is your crontab, just change the permissions to 755 which allows others to read & execute but not write. Or maybe better yet change it to 700 so only you—as the owner of the file—can do anything to the file. But avoid 777 permissions if you want to keep your system safe, stable & sane.

1 Comment

0755 is read and execute for group and other, not read and write (it's writes you want to block anyway).
0

You have two options. In the first line of your file, tell what program you want to interpret the script

#!/bin/bash
...more code...

Or in your crontab, tell what program you want to interpret the script

* * * * * bash /home/pi/MyScripts/test.sh

In this option, you do not need to make the script executable

2 Comments

@JakeGould, are either of these two commands correct then? could it be fixed by writing #!/bin/sh at the top?
@JakeGould, I made the change.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.