I have a specific script that gets the IP
address of a particular MAC
. For this it uses arp
, and it works correctly. The problem comes when I Program a crontab
to run that script; it works fine and runs but the line where it runs the arp
command does not work and, therefore the script does not finish correctly only if it's run from crontab
.
The script is:
#!/bin/bash
subred=192.168.1.0/24
mac=aa:bb:cc:dd:ff:gg
log() {...}
log info "Init program"
ip=$(nmap -sP $subred >/dev/null && arp -an | grep $mac | awk '{print $2}' | sed 's/[()]//g')
if [ $ip ]; then
log ok "IP found in $ip"
else
log error "IP not found"
fi
log info "Finished program"
This script has been configured to run on crontab
every hour with @hourly /root/machaunter.sh
. Time scheduling in cron
is well done and the script runs smoothly, ruling out permission or script issues. The proof of them is the log file it generates:
29/04/2020 14:00:01 Init program
29/04/2020 14:00:06 IP not found
29/04/2020 14:45:59 Init program
29/04/2020 14:46:08 IP found in 192.168.1.173
29/04/2020 14:46:09 Finished program
29/04/2020 15:00:01 Init program
29/04/2020 15:00:10 IP not found
29/04/2020 16:00:01 Init program
29/04/2020 16:00:13 IP not found
29/04/2020 17:00:01 Init program
29/04/2020 17:00:07 IP not found
29/04/2020 18:00:01 Init program
29/04/2020 18:00:05 IP not found
29/04/2020 18:25:43 Init program
29/04/2020 18:25:50 IP found in 192.168.1.173
29/04/2020 18:25:51 Finished program
As you can see by the hours, the two times I ran the script manually worked correctly, but the rest of the Times didn't.
I've been debugging and added to the script tests until I find that, in the arp
call does not show anything, however it does when I launch it manually (to do the tests I added the log error "arp: $(arp -an)"
log and changed the crontab to run every minute * * * * /root/machaunter.sh
)
30/04/2020 09:22:01 Init program
30/04/2020 09:22:01 arp:
30/04/2020 09:23:01 Init program
30/04/2020 09:23:01 arp:
30/04/2020 09:24:02 Init program
30/04/2020 09:24:02 arp:
30/04/2020 09:24:29 Init program
30/04/2020 09:24:29 arp: Address HWtype HWaddress Flags Mask Iface 192.168.1.46 ether 7e:2d:d1:ca:d9:c0 C br0 192.168.1.68 ether c894:66:dd:1c:c2:9d C br0 192.168.1.173 ether 48:48:59:e5:b8:5e C br0 192.168.1.1 ether bf:f1:54:4d:e3:25 C br0
30/04/2020 09:25:01 Init program
30/04/2020 09:25:01 arp:
30/04/2020 09:26:01 Init program
30/04/2020 09:26:01 arp:
30/04/2020 09:27:01 Init program
30/04/2020 09:27:01 arp:
30/04/2020 09:28:01 Init program
30/04/2020 09:28:01 arp:
30/04/2020 09:29:02 Init program
30/04/2020 09:29:02 arp:
30/04/2020 09:30:01 Init program
30/04/2020 09:30:01 arp:
30/04/2020 09:31:01 Init program
30/04/2020 09:31:01 arp:
As can be seen from the log, the arp
command never returns data except for the only time I have manually launched it. Also, as you can see when calling this command doesn't terminate the script (we do not have the Finished program
log)
Why is this happening? what solution would there be?
UPDATE WITH CRON DAEMON LOG
● cron.service - Regular background program processing daemon
Loaded: loaded (/lib/systemd/system/cron.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2020-04-27 18:09:19 UTC; 2 days ago
Docs: man:cron(8)
Main PID: 486 (cron)
CGroup: /system.slice/cron.service
└─486 /usr/sbin/cron -f
Apr 30 10:40:01 NanoPi-R1 CRON[14428]: (root) CMD (/root/machaunter.sh)
Apr 30 10:40:01 NanoPi-R1 CRON[14429]: (root) CMD ( /bin/bash /usr/bin/sync_ntp_rtc.sh /dev/rtc0)
Apr 30 10:40:01 NanoPi-R1 CRON[14421]: (CRON) info (No MTA installed, discarding output)
Apr 30 10:40:01 NanoPi-R1 CRON[14421]: pam_unix(cron:session): session closed for user root
Apr 30 10:40:04 NanoPi-R1 CRON[14420]: (CRON) info (No MTA installed, discarding output)
Apr 30 10:40:04 NanoPi-R1 CRON[14420]: pam_unix(cron:session): session closed for user root
Apr 30 10:41:01 NanoPi-R1 CRON[14459]: pam_unix(cron:session): session opened for user root by (uid=0)
Apr 30 10:41:01 NanoPi-R1 CRON[14463]: (root) CMD (/root/machaunter.sh)
Apr 30 10:41:01 NanoPi-R1 CRON[14459]: (CRON) info (No MTA installed, discarding output)
Apr 30 10:41:01 NanoPi-R1 CRON[14459]: pam_unix(cron:session): session closed for user root
UPDATE WITH STDOUT OF COMMANDS
I've added the XXXX redirect to the crontab command >/tmp/logfile 2>&1
, lagging:
* * * * * /root/machaunter.sh >/tmp/logfile 2>&1
In /tmp/logfile
I get:
30/04/2020 13:52:01 [info] Init program
/root/machaunter.sh: line 37: arp: command not found
30/04/2020 13:52:01 [info] arp:
$PATH
value that includes the location of e.g.nmap
(and the other tools you're using)? Are you getting errors emailed to you by the cron daemon, or in the cron log file?$PATH
variable and/etc/environment
is/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
.nmap
works (it's in/usr/bin
) butarp
doesn't work (it's in/usr/sbin
). How can I see whichPATH
variables havecron
?systemctl
log information @Kusalananda./tmp/logfile
the output/root/machaunter.sh: line 37: arp: command not found
@Kusalananda. Only fails witharp
, the rest of the commands work fine, could it be becausearp
is in/usr/sbin
?