0

My code didn't work in crontab. I installed new Raspberry Pi OS and write a simple shell script. It still do not work.

I added setup new Raspberry wrote a simple script hello.sh. Setup crontab run every 5 min and nothing happens.

1. Raspberry Pi OS Lite (64 bit)

pi@eve:~ $ lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description:    Debian GNU/Linux 11 (bullseye)
Release:        11
Codename:       bullseye

2. shell script: hello.sh

pi@eve:~ $ chmod 755 hello.sh
pi@eve:~ $ cat hello.sh
#!/bin/bash
# Program:
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
echo -e "Hello World! \a \n"
filename="text_file" # File name

date=$(date +%Y%m%d) # Today
file=${filename}${date} # add a file
touch "${file}"

exit 0

3.setup crontab -e

# Edit this file to introduce tasks to be run by cron.
@reboot sleep 10; /usr/bin/printenv > /home/pi/cronenv.txt 2>&1
/5 * * * * /home/pi/hello.sh > /home/pi/cron.log 2>&1

printenv

pi@eve:~ $ cat cronenv.txt
HOME=/home/pi
LOGNAME=pi
PATH=/usr/bin:/bin
LANG=en_GB.UTF-8
SHELL=/bin/sh
PWD=/home/pi

Note: Connect with SSH. Client is Windows 11 PowerShell

dos2unix hello.sh
4
  • Question is unclear. Commented Feb 21, 2023 at 20:56
  • I added setup new Raspberry wrote a simple script hello.sh. Setup crontab run every 5 min and nothing happens. Commented Feb 22, 2023 at 6:06
  • Does your script do what you expect when run directly from a shell? Commented Feb 22, 2023 at 15:56
  • print "Hollo world !" to /home/pi/cron.log and a file , text_file.yymmdd Commented Feb 22, 2023 at 17:53

1 Answer 1

1

There could be several reasons why your script is not working in crontab:

  • The crontab schedule in your Q is WRONG: /5 * * * *. Fix it.

  • Incorrect path: When you run a script from the command line, you may be in a different directory than when the script runs in crontab. Make sure that the full path to your script is specified in your crontab entry.

  • Permissions: Make sure that your script has the correct permissions to be executed by the user specified in the crontab entry. You can set the permissions by running the command "chmod +x /path/to/your/script.sh".

  • Environment variables: Cron runs in a limited environment, so any environment variables that your script needs may not be available. You can try to set these variables explicitly in your script or in the crontab entry.

  • Output redirection: If your script produces any output, make sure that it is redirected to a file, as cron does not have a terminal to display the output.

Here is an example of a crontab entry that runs the script every 5 minutes:

*/5 * * * * /path/to/your/script.sh >> /path/to/logfile.log 2>&1
3
  • Fixed crontab */5 * * * * , The issue was gone. Thanks ! Commented Feb 23, 2023 at 23:07
  • I did every as mentioned in this answer and still my script wont start at reboot. @reboot /home/pi/myscript.sh Commented Jun 23, 2023 at 20:01
  • @ThN: This answer does not address a @reboot job. My guess is that you've gotten something else wrong. Why don't you try asking a new question to make things clear? Commented Jun 23, 2023 at 22:05

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.