LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 02-18-2016, 09:54 PM   #1
cwc
Member
 
Registered: Nov 2004
Location: Tri-Cities, WA
Distribution: Gentoo, Ubuntu, Mint,Fedora
Posts: 71

Rep: Reputation: 16
crontab in Centos 7 not working.


[solved]
Running Centos 7.
I have a cron that I want to test so I am running it every minute until I have it debugged.

It does not work.

#crontab -e
* * * * * /home/cwc/scripts/logfile.sh

Here is the script which resides in /home/cwc/scripts/. The script works.

#!/bin/sh
NOW=$(date +"%Y-%m-%d_%H%M%S");
LOGFILE="log-$NOW.txt";
/usr/bin/nmap -sP 192.168.1.0/24 >> /home/cwc/html/logs/$LOGFILE;

Last edited by cwc; 02-22-2016 at 09:06 PM.
 
Old 02-18-2016, 10:05 PM   #2
JJJCR
Senior Member
 
Registered: Apr 2010
Posts: 1,812

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
It doesn't work, do you mean that the cron tab script is not executed?

It doesn't do the desired output? or what?
 
Old 02-18-2016, 10:53 PM   #3
cwc
Member
 
Registered: Nov 2004
Location: Tri-Cities, WA
Distribution: Gentoo, Ubuntu, Mint,Fedora
Posts: 71

Original Poster
Rep: Reputation: 16
I do not get the desired output.
If the script is executed manually it works but not from cron.

Thanks.
 
Old 02-18-2016, 11:34 PM   #4
JJJCR
Senior Member
 
Registered: Apr 2010
Posts: 1,812

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
Post

Quote:
Originally Posted by cwc View Post
I do not get the desired output.
If the script is executed manually it works but not from cron.

Thanks.
Have you tried running it let's say an interval of 5 minutes? Just try whether what you expect will work.
 
Old 02-22-2016, 08:26 AM   #5
fmattheus
Member
 
Registered: Nov 2015
Posts: 104

Rep: Reputation: 38
When you run things from CRON your environment variables are different then when you run them directly.

Most likely /home/cwc/scripts/logfile.sh is relying on one or more of these environment variables. Could be that PATH is different, or something that normally gets set in .bashrc ...

Edit: Ignore this comment. I missed that you posted the contents of the script. Habitual is right ...

Last edited by fmattheus; 02-22-2016 at 08:46 AM.
 
Old 02-22-2016, 08:37 AM   #6
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Scanning 254 hosts every minute of every day?

http://www.dataphyx.com/cronsandbox/cronsandboxgui.php

It takes me upward of 30 minutes to scan 192.168.1.0/24

Last edited by Habitual; 02-22-2016 at 08:38 AM.
 
Old 02-22-2016, 01:21 PM   #7
michaelk
Moderator
 
Registered: Aug 2002
Posts: 20,408

Rep: Reputation: 3551Reputation: 3551Reputation: 3551Reputation: 3551Reputation: 3551Reputation: 3551Reputation: 3551Reputation: 3551Reputation: 3551Reputation: 3551Reputation: 3551
-sP just pings the hosts and does not scan ports. It should only take seconds to complete.

Your script works as is on my CentOS 7 system from cron. How are you running it from the command line? Is it executable?
 
Old 02-22-2016, 01:34 PM   #8
imadsani
Member
 
Registered: Aug 2013
Distribution: CentOS 6.5
Posts: 64

Rep: Reputation: Disabled
stupid question, but did you restart crond after setting the cron?

Code:
service crond restart
if you've done that, make the file executable and add sh before the command

Code:
 * * * * * sh /home/cwc/scripts/logfile.sh

Last edited by imadsani; 02-22-2016 at 01:36 PM.
 
Old 02-22-2016, 01:37 PM   #9
michaelk
Moderator
 
Registered: Aug 2002
Posts: 20,408

Rep: Reputation: 3551Reputation: 3551Reputation: 3551Reputation: 3551Reputation: 3551Reputation: 3551Reputation: 3551Reputation: 3551Reputation: 3551Reputation: 3551Reputation: 3551
If you add a user job via crontab -e restarting crond is not necessary.
 
Old 02-22-2016, 03:15 PM   #10
fmattheus
Member
 
Registered: Nov 2015
Posts: 104

Rep: Reputation: 38
Adding sh before the script is also not necessary, as the first line in your batch file has specified that already.

I tried it on my computer and it seems to work as well. In order to debug it any further you need to give more info. For example, what is the expected output, what output are you getting. Are you getting a separate file for each minute. Are they all the same?

One comment is that you've specified /bin/sh, but are using bash specific commands like $(). You should use /bin/bash instead. It might work the way you've written it, but it doesn't have to ...
 
Old 02-22-2016, 03:23 PM   #11
themrrobert
Member
 
Registered: Feb 2007
Distribution: Debian Sid
Posts: 52

Rep: Reputation: 16
Try running the script using /bin/env.
Code:
* * * * * /bin/env /home/cwc/scripts/logfile.sh
This will create a fresh environment.

You can also set environment variables with env if necessary.
 
Old 02-22-2016, 03:46 PM   #12
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
-sP just pings the hosts and does not scan ports. It should only take seconds to complete.

Your script works as is on my CentOS 7 system from cron. How are you running it from the command line? Is it executable?
Thanks! I blinked and it was over.
 
Old 02-22-2016, 09:06 PM   #13
cwc
Member
 
Registered: Nov 2004
Location: Tri-Cities, WA
Distribution: Gentoo, Ubuntu, Mint,Fedora
Posts: 71

Original Poster
Rep: Reputation: 16
got things to work

*/30 * * * * /bin/bash -l /home/cwc/bash/makelogs.sh


script:

#!/bin/bash
NOW=$(date +"%Y-%m-%d-%T");
LOGFILE="log-$NOW.txt";
/usr/bin/nmap -sP 192.168.1.0/24 > /home/cwc/html/logs/$LOGFILE;



Thanks for all the tips.
 
Old 02-23-2016, 12:41 AM   #14
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Kubuntu x64, Raspbian, CentOS
Posts: 1,861
Blog Entries: 36

Rep: Reputation: 459Reputation: 459Reputation: 459Reputation: 459Reputation: 459
You could use "#!/bin/bash -l" on the first line of your script. Though I see nothing that would require you to use the login option. It's good to read "man 5 crontab" and "man crontab" to understand how the execution occurs including the environment.

Last edited by sag47; 02-23-2016 at 12:43 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Crontab weird behavior on CentOS 5.5 SINEKT Linux - General 9 10-21-2013 02:40 AM
Crontab is not working properly on CentOS 6.3 miguelangeljma Linux - Newbie 12 09-30-2012 10:56 PM
[SOLVED] Centos 6 Crontab NOT runing Ubunter Linux - Server 16 04-30-2012 07:45 AM
Crontab is not working, the script is working arfal SUSE / openSUSE 6 02-08-2010 08:48 PM
script won't run in crontab(centos 5) ncsuapex Programming 2 03-09-2008 06:55 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 03:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration