User avatar
Trigo
Posts: 106
Joined: Fri Jun 17, 2016 7:52 pm

Crontab on raspian

Sat Mar 18, 2017 5:39 pm

i'm to setting up a raspian running a cloud storage service and i want it to run a script that sees if the service is up and running! I've made the script but it doesn't work with crontab! Can someone help?
Beat psychologically with a stick :?:

User avatar
Trigo
Posts: 106
Joined: Fri Jun 17, 2016 7:52 pm

Re: Crontab on raspian

Sat Mar 18, 2017 8:24 pm

This is the script
#!/bin/sh
APP=$(ps aux | grep -v grep | grep apl)
if [ -z "$APP" ];
then
echo "Restart apl-daemon"
apl daemon
apl start --config /home/pi/path/to/config1.json
apl start --config /home/pi/path/to/config2.json
apl start --config /home/pi/path/to/config2.json
fi
This is the output from crontab-l
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow command
*/ 5 * * * pi pi /home/pi/watchdog.sh
@reboot pi pi /home/pi/watchdog.sh

File was written on /tmp/crontab.Mi9kwE/crontab
This is the output from cron status
pi@raspberrypi:~ $ systemctl status cron
● cron.service - Regular background program processing daemon
Loaded: loaded (/lib/systemd/system/cron.service; enabled)
Active: active (running) since Sat 2017-03-18 14:13:22 UTC; 6h ago
Docs: man:cron(8)
Main PID: 454 (cron)
CGroup: /system.slice/cron.service
└─454 /usr/sbin/cron -f
pi@raspberrypi:~ $
What should i do! Maybe changing the ownership or the permission of the script? Who can help? I'm really noob at this!
Beat psychologically with a stick :?:

User avatar
Paeryn
Posts: 3045
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Crontab on raspian

Sat Mar 18, 2017 8:52 pm

Why have you got pi pi in the crontab? There should be 5 paramaters specifiying the time & date (or the specific @reboot taking place of the time & date parameters) and then the program (with arguments) to run. As you have it it will try to run a program called pi with its first argument as pi and your script's filename as the second argument.
She who travels light — forgot something.
Please note that my name doesn't start with the @ character so can people please stop writing it as if it does!

User avatar
Trigo
Posts: 106
Joined: Fri Jun 17, 2016 7:52 pm

Re: Crontab on raspian

Sat Mar 18, 2017 8:59 pm

solved! it looks like there was much space between this* :)
Beat psychologically with a stick :?:

User avatar
Trigo
Posts: 106
Joined: Fri Jun 17, 2016 7:52 pm

Re: Crontab on raspian

Sat Mar 18, 2017 10:57 pm

i'm not able to make it run! :oops: I've got two crontabs one under root and other under pi!
Beat psychologically with a stick :?:

User avatar
Paeryn
Posts: 3045
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Crontab on raspian

Sat Mar 18, 2017 11:58 pm

Trigo wrote:i'm not able to make it run! :oops: I've got two crontabs one under root and other under pi!
What do your crontab lines look like now? Did you remove the errornous pi pi that you originally had?

What directory is the apl program in, is it in one of the directories that are listed in the PATH variable in the relavent crontab?

Try redirecting stdout & stderr for the command in the crontab so you can see any error messages.

Code: Select all

* * * * * /home/pi/watchdog.sh >/home/pi/watchdog.output 2>&1
She who travels light — forgot something.
Please note that my name doesn't start with the @ character so can people please stop writing it as if it does!

User avatar
Trigo
Posts: 106
Joined: Fri Jun 17, 2016 7:52 pm

Re: Crontab on raspian

Sun Mar 19, 2017 12:00 am

Like this
pi@raspberrypi:~ $ systemctl status cron
● cron.service - Regular background program processing daemon
Loaded: loaded (/lib/systemd/system/cron.service; enabled)
Active: active (running) since Sat 2017-03-18 23:48:56 UTC; 7min ago
Docs: man:cron(8)
Main PID: 454 (cron)
CGroup: /system.slice/cron.service
└─454 /usr/sbin/cron -f
pi@raspberrypi:~ $ sudo su
root@raspberrypi:/home/pi# systemctl status cron
● cron.service - Regular background program processing daemon
Loaded: loaded (/lib/systemd/system/cron.service; enabled)
Active: active (running) since Sat 2017-03-18 23:48:56 UTC; 8min ago
Docs: man:cron(8)
Main PID: 454 (cron)
CGroup: /system.slice/cron.service
└─454 /usr/sbin/cron -f

Mar 18 23:50:09 raspberrypi CRON[2144]: (CRON) info (No MTA installed, discarding output)
Mar 18 23:50:09 raspberrypi CRON[2144]: pam_unix(cron:session): session closed for user root
Mar 18 23:55:01 raspberrypi CRON[2234]: pam_unix(cron:session): session opened for user root by (uid=0)
Mar 18 23:55:01 raspberrypi CRON[2233]: pam_unix(cron:session): session opened for user root by (uid=0)
Mar 18 23:55:01 raspberrypi CRON[2241]: (root) CMD (( cd /home/pi/ && sh watchdog.sh ))
Mar 18 23:55:01 raspberrypi CRON[2242]: (root) CMD ( /home/pi/watchdog.sh)
Mar 18 23:55:01 raspberrypi CRON[2234]: (CRON) info (No MTA installed, discarding output)
Mar 18 23:55:01 raspberrypi CRON[2233]: (CRON) info (No MTA installed, discarding output)
Mar 18 23:55:01 raspberrypi CRON[2234]: pam_unix(cron:session): session closed for user root
Mar 18 23:55:01 raspberrypi CRON[2233]: pam_unix(cron:session): session closed for user root
root@raspberrypi:/home/pi# exit
exit
pi@raspberrypi:~ $ storj status

daemon is not running, try: storj daemon
Last edited by Trigo on Sun Mar 19, 2017 12:25 am, edited 1 time in total.
Beat psychologically with a stick :?:

User avatar
Trigo
Posts: 106
Joined: Fri Jun 17, 2016 7:52 pm

Re: Crontab on raspian

Sun Mar 19, 2017 12:12 am

where do i find watchdog.output? how do i view the output?
Beat psychologically with a stick :?:

User avatar
Paeryn
Posts: 3045
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Crontab on raspian

Sun Mar 19, 2017 12:23 am

You just gave the status of cron, I asked what your crontab looked like (as reported with crontab -l).

I gave watchdog.output as an example filename that would contain the output of the attempted running of your script. It would be created when cron tries running your script and as I explicitly gave the full path to it as being in pi's home directory that is where it would be.

You can view the file when it has been created with whatever you want, or

Code: Select all

cat /home/pi/watchdog.output
She who travels light — forgot something.
Please note that my name doesn't start with the @ character so can people please stop writing it as if it does!

User avatar
Trigo
Posts: 106
Joined: Fri Jun 17, 2016 7:52 pm

Re: Crontab on raspian

Sun Mar 19, 2017 12:27 am

Paeryn wrote:You just gave the status of cron, I asked what your crontab looked like (as reported with crontab -l).

I gave watchdog.output as an example filename that would contain the output of the attempted running of your script. It would be created when cron tries running your script and as I explicitly gave the full path to it as being in pi's home directory that is where it would be.

You can view the file when it has been created with whatever you want, or

Code: Select all

cat /home/pi/watchdog.output
I'm seeing it! But i'm connected to the pi via ssh! And i would like to open it... i've tried with nano and with less!
Beat psychologically with a stick :?:

User avatar
Trigo
Posts: 106
Joined: Fri Jun 17, 2016 7:52 pm

Re: Crontab on raspian

Sun Mar 19, 2017 12:28 am

Paeryn wrote:You just gave the status of cron, I asked what your crontab looked like (as reported with crontab -l).

I gave watchdog.output as an example filename that would contain the output of the attempted running of your script. It would be created when cron tries running your script and as I explicitly gave the full path to it as being in pi's home directory that is where it would be.

You can view the file when it has been created with whatever you want, or

Code: Select all

cat /home/pi/watchdog.output
It shows nothing...! :?:
Beat psychologically with a stick :?:

User avatar
Paeryn
Posts: 3045
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Crontab on raspian

Sun Mar 19, 2017 12:37 am

If it's an empty file then no output was generated from the script. Since your script echos some text when it should be running apl then it looks like it hasn't got there. Try putting one right at the start so you can see that the script is at least running. And maybe one to print the value of $APP too.
She who travels light — forgot something.
Please note that my name doesn't start with the @ character so can people please stop writing it as if it does!

User avatar
TracyLBaker
Posts: 41
Joined: Sat Feb 18, 2017 3:44 pm
Location: Phoenix, AZ

Re: Crontab on raspian

Sun Mar 19, 2017 1:08 am

Trigo wrote:i'm not able to make it run! :oops: I've got two crontabs one under root and other under pi!
Each user gets their own crontab. The system has one as well (which is /etc/crontab and edited by hand, not crontab -l).

Users crontabs should look like this (6 fields):
minute hour day_of_month month day_of_week command

The system's crontab (/etc/crontab), has 7 fields and is edited with a text file editor:
minute hour day_of_month month day_of_week user_to_run_command_as command

Some rules:
  • Spaces make a difference -- spacing doesn't.
    Scripts must be executable.
    Full paths to the script should be used in the crontab.
    The script should use full paths to call the other scripts and executables (other than builtins) it uses.
    Cronjobs cannot output stdout or stderr (to the terminal screen) as they are background jobs and have no control over the terminal.
RHCE, Red Hat Academy Professor - Estrella Mountain Community College
2 Pi 4B 4Gb media servers with 48Tb 5 Pi 3 Model B+s: 1 headless media/NFS/SMB server - Raspbian Jessie, 1 Kali Linux w/fruitywifi, 1 headless CentOS 7 webserver, 2 backups

User avatar
rpdom
Posts: 17542
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Crontab on raspian

Sun Mar 19, 2017 8:46 am

To log the output of your cron scripts, you should send the output somewhere (cron defaults to trying to mail it, but there is no mail set up for it).

Try

Code: Select all

# m h dom mon dow command
*/ 5 * * * /home/pi/watchdog.sh >>/tmp/watchdog.log 2>&1
@reboot /home/pi/watchdog.sh >>/tmp/watchdog.log  2>&1
and then look at the contents of /tmp/watchdog.log to see why it didn't do what was expected.
[edit]fixed redirection typo
Last edited by rpdom on Sun Mar 19, 2017 12:48 pm, edited 1 time in total.

User avatar
Paeryn
Posts: 3045
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Crontab on raspian

Sun Mar 19, 2017 11:59 am

rpdom wrote:To log the output of your cron scripts, you should send the output somewhere (cron defaults to trying to mail it, but there is no mail set up for it).

Try

Code: Select all

# m h dom mon dow command
*/ 5 * * * /home/pi/watchdog.sh 2>&1 >>/tmp/watchdog.log
@reboot /home/pi/watchdog.sh  2>&1 >>/tmp/watchdog.log
and then look at the contents of /tmp/watchdog.log to see why it didn't do what was expected.
You've got the redirections in the wrong order, you have to redirect stdout to a file before redirecting stderr to stdout. Putting 2>&1 before the file redirection will just result in stderr being redirected to the original stdout, not the subsequent file.
She who travels light — forgot something.
Please note that my name doesn't start with the @ character so can people please stop writing it as if it does!

User avatar
DougieLawson
Posts: 39787
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Crontab on raspian

Sun Mar 19, 2017 12:34 pm

Start with
sudo crontab -l
crontab -l
cat /etc/crontab
cat /etc/cron.d/*
cat /etc/cron.daily/*
cat /etc/cron.weekly/*
cat /etc/cron.monthly/*


There's lot of places to put cron's tables.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All fake doctors are on my foes list.

Return to “General programming discussion”