Using crontab to run my script at fix time - linux

In the Crontab -e
I have added the code:
48 17 * * 1-5 /home/user/cript.sh >/dev/null 2>&1
in the system logs I receive: /var/log/syslog
Nov 9 17:48:01 HP-Pavilion cron[963]: (user) RELOAD (crontabs/user)
Nov 9 17:48:01 HP-Pavilion CRON[18951]: (user) CMD (/home/user/cript.sh >/dev/null 2>&1)
Nov 9 17:48:02 HP-Pavilion: [34529.848963] ieee80211 phy0: rt2x00queue_flush_queue: Warning - Queue 2 failed to flush
Nov 9 17:48:02 HP-Pavilion kernel: [34530.360969] ieee80211 phy0: rt2x00queue_flush_queue: Warning - Queue 2 failed to flush
Nov 9 17:48:03 HP-Pavilion kernel: [34530.872983] ieee80211 phy0: rt2x00queue_flush_queue: Warning - Queue 2 failed to flush
Nov 9 17:48:04 HP-Pavilion kernel: [34532.021114] ieee80211 phy0: rt2x00queue_flush_queue: Warning - Queue 2 failed to flush
Nov 9 17:48:05 HP-Pavilion kernel: [34532.581126] ieee80211 phy0: rt2x00queue_flush_queue: Warning - Queue 2 failed to flush
my script contains a command line:
./a.out -f abc_`date+"%Y%m%d"`.txt
Now why won't the file gets created ???
When I start this program using Crontab ?
If I just run the script directly from the command line as:
./cript.sh
then it runs fine and a file gets created but If i put it in the Crontab, the output file doesn't get created. What could be the issues.
I am beginner at crontab.
Thanks

Re-direct the output to some file instead of /dev/null, check for the file once the script is executed.
you may b able to figure out what the problem is.

Related

cron.hourly script is only run manually

It does not seem that file I put to /etc/cron.hourly does not seem to work
It's called cron_hourly_homepage, so it does not seem that its filename issue. Ownership seems alright as well: -rwxr-xr-x
run-parts --test /etc/cron.hourly sees this file, run-parts --report /etc/cron.hourly can run it as well
Would like to get it fixed, so no crontab recommendations are necessary :D
● cron.service - Regular background program processing daemon
Loaded: loaded (/lib/systemd/system/cron.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2021-12-27 20:20:40 GMT; 1 day 23h ago
Docs: man:cron(8)
Main PID: 401 (cron)
Tasks: 1 (limit: 4915)
CPU: 9.213s
CGroup: /system.slice/cron.service
└─401 /usr/sbin/cron -f
Dec 29 17:19:01 raspberrypi CRON[9255]: (CRON) info (No MTA installed, discarding output)
Dec 29 17:19:01 raspberrypi CRON[9255]: pam_unix(cron:session): session closed for user root
Dec 29 18:17:01 raspberrypi CRON[9427]: pam_unix(cron:session): session opened for user root(uid=0) by (uid=0)
Dec 29 18:17:01 raspberrypi CRON[9428]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly)
It is common to forget cron scripts are running without a shell and no environment context.
Please see this answer

cron stop sending error mails if command output is redirected to logfile

I have a problem with the error mail's from cron
If I create two job's executing the same perl script one which redirected to /dev/null or a logfile and one without redirect I only get an error mail for the one without redirect
/etc/cron.d/test-cron
MAILTO="logs#example.com"
* * * * * root /root/test.sh > /dev/null
* * * * * root /root/test.sh
/root/test.sh
#!/usr/bin/perl
use strict;
print "test\n";
exit 1;
syslog output for cron and postfix
May 18 19:14:01 cron-master CRON[31428]: (root) CMD ([31436] /root/test.sh)
May 18 19:14:01 cron-master CRON[31428]: (CRON) error (grandchild #31436 failed with exit status 1)
May 18 19:14:01 cron-master CRON[31428]: (root) END ([31436] /root/test.sh)
May 18 19:14:01 cron-master CRON[31429]: (root) CMD ([31439] /root/test.sh > /dev/null)
May 18 19:14:01 cron-master CRON[31429]: (CRON) error (grandchild #31439 failed with exit status 1)
May 18 19:14:01 cron-master CRON[31429]: (root) END ([31439] /root/test.sh > /dev/null)
May 18 19:14:01 cron-master postfix/pickup[28859]: 5537251A9: uid=0 from=<root>
May 18 19:14:01 cron-master postfix/cleanup[30966]: 5537251A9: message-id=<20200518191401.5537251A9#cron-master#example.com>
May 18 19:14:01 cron-master postfix/qmgr[143]: 5537251A9: from=<cron-master#example.com>, size=674, nrcpt=1 (queue active)
May 18 19:14:01 cron-master postfix/smtp[30968]: 5537251A9: to=<logs#example.com>, relay=smtp.example.com[80.50.67.97]:587, delay=0.42, delays=0.02/0/0.32/0.09, dsn=2.0.0, status=sent (250 Requested mail action okay, completed: id=1Ma1oK-1jXP8H2tyW-00W08q)
May 18 19:14:01 cron-master postfix/qmgr[143]: 5537251A9: removed
cron -> 3.0pl1-136ubuntu1
postfix -> 3.4.10-1ubuntu1
OS -> latest Ubuntu 20.04 docker image (ubuntu:focal-20200423)
Docker Endpoint -> /usr/sbin/cron -f -l -L 15
You can use tee command to "fork" standard output.
* * * * * root /root/test.sh | tee -a logfile_name
man tee
tee - read from standard input and write to standard output and files
Synopsis
tee [OPTION]... [FILE]...
Description
Copy standard input to each FILE, and also to standard output.
-a, --append
append to the given FILEs, do not overwrite

Debian init.d script fails to sleep

This problem occurs on a Pogoplug E02 running Debian jessie.
At startup the network interface takes several seconds to come online. A short delay is required after the "networking" script completes to ensure that ensuing network operations occur properly.
I wrote the following script and inserted it using update-rc.d. The script inserted correctly and executes at boot time in proper sequence, after networking and before the network-dependent scripts which were modified to depend on netdelay
cat /etc/init.d/netdelay
#! /bin/sh
### BEGIN INIT INFO
# Provides: netdelay
# Required-Start: networking
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Delay 5s after eth0 up for Pogoplug
# Description:
### END INIT INFO
PATH=/sbin:/usr/sbin:/bin:/usr/bin
./lib/init/vars.sh
./lib/lsb/init-functions
log_action_msg "Pausing for eth0 to come online"
/bin/sleep 5
log_action_msg "Continuing"
exit 0
When the script executes at startup there is no delay. I've used both sleep and /bin/sleep in the script but neither effect the desired delay. Boot log showing this attached below.
Thu Jan 1 00:00:25 1970: Configuring network interfaces...done.
Thu Jan 1 00:00:25 1970: INIT: Entering runlevel: 2
Thu Jan 1 00:00:25 1970: Using makefile-style concurrent boot in runlevel 2.
Thu Jan 1 00:00:26 1970: Starting SASL Authentication Daemon: saslauthd.
Thu Jan 1 00:00:29 1970: Pausing for eth0 to come online.
Thu Jan 1 00:00:30 1970: Continuing.
Thu Jan 1 00:00:33 1970: ntpdate updating system time.
Wed Feb 1 05:33:40 2017: Starting enhanced syslogd: rsyslogd.
(The Pogoplug has no hardware clock and has no idea what time it is until ntpdate has run.)
Can someone see where the problem might be?

Cronjob script trouble launching application

I started with cronjobs a while ago, but up until yesterday I've run into a problem I can't figure/find out.
#reboot me /etc/application/start-script.sh
I have Raspbian Jessie (minimal) installed on a Raspberry Pi Zero. One of the users has a cronjob command #reboot. When I check "sudo /etc/init.d/cron status", I can see the cronjob is picked up after a reboot and executed. The only thing is that any output is dropped, the "No MTA installed"-message, (care?).
#!/bin/bash
# My start script
logfile=/home/me/logfile.log
echo "Starting program..." >> $logfile
application
echo "Program started!" >> $logfile
As you can see, it should create a log file, and it does this after a reboot when the script is called as a cronjob. This script works perfectly fine when you manualy execute it, it writes the output to the logfile AND starts the program.
The problem is: the program is not launched when the .sh script is called as a cronjob.
Why is only the application not started when the script is executed???
"sudo /etc/init.d/cron status" output
Mar 17 22:14:45 pizza-pi systemd[1]: Starting Regular background program processing daemon...
Mar 17 22:14:45 pizza-pi systemd[1]: Started Regular background program processing daemon.
Mar 17 22:14:45 pizza-pi cron[292]: (CRON) INFO (pidfile fd = 3)
Mar 17 22:14:45 pizza-pi cron[292]: (CRON) INFO (Running #reboot jobs)
Mar 17 22:14:45 pizza-pi CRON[296]: pam_unix(cron:session): session opened for user me by (uid=0)
Mar 17 22:14:45 pizza-pi CRON[318]: (me) CMD (etc/application/start-script.sh)
Mar 17 22:14:45 pizza-pi CRON[296]: (CRON) info (No MTA installed, discarding output)
Mar 17 22:14:45 pizza-pi pam_unix(cron:session): session closed for user me
Edit the /etc/rc.local file and add the following line in /etc/init.d/cron/start be sure that it should before exit 0.
Follow this link https://rahulmahale.wordpress.com/2014/09/03/solved-running-cron-job-at-reboot-on-raspberry-pi-in-debianwheezy-and-raspbian/
Hope answer is useful for you

Automatically launching Firefox from terminal using at command

I am a beginner at linux and really enthusiastic to learn the OS. I am trying to launch Firefox(or any other software like Evince) from the command line as follows:
[root#localhost ~]# at 1637
[root#localhost ~]# at> firefox
[root#localhost ~]# at> ^d
The job gets scheduled without any error. But at the specified time it does not run.
I also tried giving the following path:
[root#localhost ~]# at 1637
[root#localhost ~]# at> /usr/bin/firefox
[root#localhost ~]# at> ^d
Still no result. But When I try to use echo to display a text on the screen it appears at the specified time as desired. What might be the issue?
I think you have not set DISPLAY. at will run in separate shell where display is not set.
try the following code.
dinesh:~$ at 2120
warning: commands will be executed using /bin/sh
at> export DISPLAY=:0
at> /usr/bin/firefox > firefox.log 2>&1
at> <EOT>
job 7 at Tue Mar 11 21:20:00 2014
If it is still failing check firefox.log for more information.
1) Its not always recommended to run things as root
2) You can also try ./firefox if you are in the current directory of firefox. In linux you need to pay attention to your path variable. Unless . (the current directory) is in your path you will have to type ./program if the program is in the same directory as you.
Also you need to pay attention to file permissions: In linux you have read-write-eXecute access.
ls -l will do a list of directories and show the file permissions:
drwxr-xr-x 10 user staff 340 Oct 6 2012 GlassFish_Server/
drwx------# 15 jeffstein staff 510 Oct 6 15:01 Google Drive/
drwxr-xr-x 20 jeffstein staff 680 May 14 2013 Kindle/
drwx------+ 67 jeffstein staff 2278 Jan 26 14:22 Library/
drwx------+ 19 jeffstein staff 646 Oct 23 18:28 Movies/
drwx------+ 15 jeffstein staff 510 Jan 3 20:29 Music/
drwx------+ 90 jeffstein staff 3060 Mar 9 20:23 Pictures/
drwxr-xr-x+ 6 jeffstein staff 204 Nov 3 21:16 Public/
drwxr-xr-x 22 jeffstein staff 748 Jan 14 2012 androidTools/
-rwxrwxrwx 1 jeffstein staff 1419 Aug 28 2013 color.sh*
This is an example of ls -l here you can see color.sh has -rwxrwxrwx that means that anybody can read or write or run the file.
Without actually knowing where you installed firefox however I can't be of more help but these are some small pointers which might help.
try finding where firefox is actually installed using "whereis firefox" command.
Then try using that path in at command.
In order to get directions on how to use a command type:
man at
this will display the "manual"
DESCRIPTION
The at and batch utilities read commands from standard input or a speci-
fied file. The commands are executed at a later time, using sh(1).
at executes commands at a specified time;
atq lists the user's pending jobs, unless the user is the superuser;
in that case, everybody's jobs are listed;
atrm deletes jobs;
batch executes commands when system load levels permit; in other words,
when the load average drops below _LOADAVG_MX (1.5), or the value
specified in the invocation of at run.
So obviously you need to schedule a job with at and you can see if it worked with atq
Read the manual and it should help - if i have more time I'll write you a quick example.

Resources