0

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:
7
  • 1
    Does the crontab environment have a $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?
    – Kusalananda
    Apr 30, 2020 at 10:29
  • 1
    It is always (to a first approximation) the environment. crontab does not read your profile. PATH is likely only "/bin:/usr/bin" (may include /sbin for root). Apr 30, 2020 at 10:37
  • My $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) but arp doesn't work (it's in /usr/sbin). How can I see which PATH variables have cron? Apr 30, 2020 at 10:46
  • I updated the post with systemctl log information @Kusalananda. Apr 30, 2020 at 10:49
  • 1
    yes, I receive in the file /tmp/logfile the output /root/machaunter.sh: line 37: arp: command not found @Kusalananda. Only fails with arp, the rest of the commands work fine, could it be because arp is in /usr/sbin? Apr 30, 2020 at 13:54

2 Answers 2

3

The environment that cron is running your script in has a different value for the PATH variable compared to your ordinary interactive environment.

This means that your script does not know where the arp command is to be found, for example (as mentioned in comments).

I would suggest that you make a note in what directories the tools that you use in your script by running command -v for each of them in an interactive shell, e.g.

command -v nmap
command -v arp

etc.

This will give you a list of pathnames for those commands. Take the directory names of these and add them to PATH in the script itself (somewhere at the start of the script):

PATH=$PATH:/some/directory/path:/another/directory/path

Doing this in the script guarantees that the tool will be found by the script.

In the end, it may be that all you need is to add /usr/sbin:

PATH=$PATH:/usr/sbin

The other alternative is to use the tools wih their absolute path, like using /usr/sbin/arp instead of just arp.

1

To run arp script in crontab, it is necessary to use the arp path as /sbin/arp instead of arp, because crontab run scripts located in /bin by default, while arp is located in /sbin/.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .