[SOLVED] Nmap not working when implemented in crontab
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am trying to scan the network every day at a particular time(below example at 12:18) by using cronjobs and then output the IPs in a file. The command works perfectly but does not work if implemented in cronjobs. The following is implemented in /etc/crontab:
I am trying to scan the network every day at a particular time(below example at 12:18) by using cronjobs and then output the IPs in a file. The command works perfectly but does not work if implemented in cronjobs. The following is implemented in /etc/crontab:
...so six parameters. You have, as a COMMAND, the word 'root', followed by the nmap statement, with lots of other commands after it. Personally, I'd put the whole nmap command with the pipes in a small script file, and let cron execute that.
...so six parameters. You have, as a COMMAND, the word 'root', followed by the nmap statement, with lots of other commands after it. Personally, I'd put the whole nmap command with the pipes in a small script file, and let cron execute that.
In /etc/crontab there is a row called user. In fact other cronjobs work, only the nmap cronjob does not work. I tried it with a script as well but nothing happens in the cronjob. How can I check the the cron logs?
Sorry, no. That is not the crontab file that gets executed, but part of the crontab SYSTEM. If you edited that file...don't, and remove any other stuff you have in there. Cron tabs are typically in /var/spool/cron/xxx (depends on the distro...which you haven't told us)
Quote:
In fact other cronjobs work, only the nmap cronjob does not work. I tried it with a script as well but nothing happens in the cronjob.
If you put the cron job in that file, I'm hardly surprised it doesn't work, especially if you continue to put "root" in front of it. You were given the syntax of the Linux cron statements; either follow them or you won't get things to work. And what are you doing to put cron jobs in? If you're not running "crontab -e" (as root, for the root cron), then you're not doing it correctly. There is AMPLE documentation about this, how to edit crontabs, and the syntax for them.
You say you tried with a script..how about telling us how you tried it, what you put in the script, and the results??
Quote:
How can I check the the cron logs?
You should have something fairly obvious in your log directory, either marked "cron", "chrony", etc. Depends on the version/distro of Linux, and how you, as the administrator, configured the system. You can also check the output file of any commands.
If you're putting that "root" in, it won't work. If you're putting this in /etc/crontab, it won't work, period.
In /etc/crontab there is a row called user. In fact other cronjobs work, only the nmap cronjob does not work. I tried it with a script as well but nothing happens in the cronjob. How can I check the the cron logs?
The crontab entry is still not correct....
Code:
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
Your entry is missing the day of week field (which I'm guessing should be "*").
Sorry, but are you not reading the replies you're getting??? You've been asked several questions, such as how you're editing the crontab, what version/distro of Linux, etc. You were given the EXACT SYNTAX to use, but seem to be ignoring it. You were asked about the script you said you tried, but don't tell us anything about that, either. If you're not going to answer questions or take the advice given, there's little point in posting a question.
AGAIN, the syntax is [Minute] [hour] [Day_of_the_Month] [Month_of_the_Year] [Day_of_the_Week] [command]
SIX parameters, period. jpollard even provided a diagram. You are *STILL* putting 'root' in front of the command, which is **NOT VALID** and will not run, for obvious reasons, the main one being there IS no 'root' command.
I must admit here that I am confused. I can see nothing fundamentally wrong with the crontab entry.
In my opinion there is no weekday missing and in my own experience adding entries to /etc/crontab does technically work (and must contain the user, in this case root).
I agree 100% with TBOne that it is bad style and should be avoided, but technically it should work.
The initial thought I had when looking at the question was: Where is HostList supposed to end up? I would specifiy this file with an absolute path.
Also: I generally try to avoid long commands with pipes and redirects and such in crontab entries. Write a script that does the job (even if it is a oneliner) to make things easier to debug.
You can than add logging etc. to find out what's going wrong. Also be aware that error output might go to stderr, so if you want to redirect it to a log file you need to do something like:
Code:
command > logfile 2>&1
Also be aware that sometimes the commands you can use in an interactive shell don't work in cron because the PATH variable is different.
I must admit here that I am confused. I can see nothing fundamentally wrong with the crontab entry.
In my opinion there is no weekday missing and in my own experience adding entries to /etc/crontab does technically work (and must contain the user, in this case root).
I agree 100% with TBOne that it is bad style and should be avoided, but technically it should work.
...providing that the CRON service it stopped/restarted to pick it up, since adding it there without the use of crontab -e won't do it automatically. This is one of the things that (as you said) is DOABLE, but shouldn't be done. You *CAN* have a service start automatically through the use of script files, etc....but why? Use systemd or init.d as they were intended.
Quote:
The initial thought I had when looking at the question was: Where is HostList supposed to end up? I would specifiy this file with an absolute path.
Also: I generally try to avoid long commands with pipes and redirects and such in crontab entries. Write a script that does the job (even if it is a oneliner) to make things easier to debug. You can than add logging etc. to find out what's going wrong. Also be aware that error output might go to stderr, so if you want to redirect it to a log file you need to do something like:
Code:
command > logfile 2>&1
Also be aware that sometimes the commands you can use in an interactive shell don't work in cron because the PATH variable is different.
Yep...OP was advised to use a script as well, and said they tried it, but didn't provide details.
Small addition for the "user" part: Here is the crontab of the system I am posting from. (I am not sure if it was edited since the install, but it should be mainly debian default content...)
Code:
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# m h dom mon dow user command
17 * * * * root cd / && run-parts --report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
Small addition for the "user" part: Here is the crontab of the system I am posting from. (I am not sure if it was edited since the install, but it should be mainly debian default content...)
Code:
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# m h dom mon dow user command
17 * * * * root cd / && run-parts --report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
Thank you for your replies guys. Sorry for causing you all this trouble I am new to this stuff. I inserted the following crontab in sudo crontab -e
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.