Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
crontab not working ? [solved]
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Networking & Security
View previous topic :: View next topic  
Author Message
cwc
Veteran
Veteran


Joined: 20 Mar 2006
Posts: 1275
Location: Tri-Cities, WA USA

PostPosted: Thu Feb 18, 2016 9:48 am    Post subject: crontab not working ? [solved] Reply with quote

I'm trying to scan my network with the following crontab?

I type crontab -e and edit the file to test for every minute:
* * * * * /bin/bash /home/cwc/bash/logfile.sh


This is the script I'm trying to run and it works just using command line:
#!/bin/sh
NOW=$(date +"%Y-%m-%d-%S");
LOGFILE="log-$NOW.txt";
nmap -sP 192.168.1.0/24 > /home/cwc/html/logs/$LOGFILE;
_________________
Without diversity there can be no evolution:)


Last edited by cwc on Sat Feb 20, 2016 5:28 pm; edited 2 times in total
Back to top
View user's profile Send private message
Syl20
l33t
l33t


Joined: 04 Aug 2005
Posts: 619
Location: France

PostPosted: Thu Feb 18, 2016 10:11 am    Post subject: Re: crontab not working ? Reply with quote

Is cron daemon started ?

Off-topic, but :
cwc wrote:
* * * * * /bin/bash /home/cwc/bash/logfile.sh
(...)
#!/bin/sh
Back to top
View user's profile Send private message
cwc
Veteran
Veteran


Joined: 20 Mar 2006
Posts: 1275
Location: Tri-Cities, WA USA

PostPosted: Thu Feb 18, 2016 1:33 pm    Post subject: Re: crontab not working ? Reply with quote

CneGroumF wrote:
Is cron daemon started ?

Off-topic, but :
cwc wrote:
* * * * * /bin/bash /home/cwc/bash/logfile.sh
(...)
#!/bin/sh


I think so:

ciclo ~ # /etc/init.d/cronie start
* WARNING: cronie is already starting
ciclo ~ # ps ax | grep cronie
15185 pts/0 S+ 0:00 grep --colour=auto cronie

I assumed I was using cronie?
_________________
Without diversity there can be no evolution:)
Back to top
View user's profile Send private message
szatox
Advocate
Advocate


Joined: 27 Aug 2013
Posts: 3117

PostPosted: Thu Feb 18, 2016 5:30 pm    Post subject: Reply with quote

Is the script executable?
Is $PATH provided for your script? (try #! /bin/bash -l )
Back to top
View user's profile Send private message
cwc
Veteran
Veteran


Joined: 20 Mar 2006
Posts: 1275
Location: Tri-Cities, WA USA

PostPosted: Fri Feb 19, 2016 12:09 am    Post subject: Reply with quote

szatox wrote:
Is the script executable?
Is $PATH provided for your script? (try #! /bin/bash -l )


This runs fine from a normal user:

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


One thing I did notice is it will run as a cron once if I restart cronie.
_________________
Without diversity there can be no evolution:)
Back to top
View user's profile Send private message
gordonb3
Apprentice
Apprentice


Joined: 01 Jul 2015
Posts: 185

PostPosted: Fri Feb 19, 2016 9:34 am    Post subject: Reply with quote

Looks like you're running systemd. That rather cryptic message means that you can't use the initd scripts on such a system. You need to use systemctl to control the services and units. It does seem that your cron daemon stops after running your job once. You should probably investigate if it keeps running when you remove certain jobs.
Back to top
View user's profile Send private message
Syl20
l33t
l33t


Joined: 04 Aug 2005
Posts: 619
Location: France

PostPosted: Fri Feb 19, 2016 10:20 am    Post subject: Re: crontab not working ? Reply with quote

gordonb3 wrote:
Looks like you're running systemd.

Ah ah. As I don't know if so, I'll assume below that you use openrc. If not, forget my post, it will be useless for you.

cwc wrote:
ciclo ~ # ps ax | grep cronie
15185 pts/0 S+ 0:00 grep --colour=auto cronie

No, cronie isn't started. The lonely related process is the "grep" command you just launched.

As rc says the service is started, but the process isn't, you need to reintialize the status of the service, to be able to restart it.
Code:
# service cronie zap
# service cronie start


Quote:
I assumed I was using cronie?

If the /etc/init.d/cronie script exists, I think so. To be sure :
Code:
# equery l '*cron*'
Back to top
View user's profile Send private message
cwc
Veteran
Veteran


Joined: 20 Mar 2006
Posts: 1275
Location: Tri-Cities, WA USA

PostPosted: Fri Feb 19, 2016 1:26 pm    Post subject: Reply with quote

Thanks for the lines:
Ya . I'm one of the evil ones running systemd

# systemctl | grep cronie
cronie.service loaded active running Command Scheduler


Here's what I have:
Code:


# service cronie zap
 * Manually resetting cronie to stopped state
ciclo ~ # service cronie start
 * WARNING: cronie is already starting
ciclo ~ # equery l '*cron*'
 * Searching for *cron* ...
[IP-] [  ] sys-process/cronbase-0.3.7-r1:0
[IP-] [  ] sys-process/cronie-1.5.0:0
ciclo ~ #





_________________
Without diversity there can be no evolution:)
Back to top
View user's profile Send private message
cwc
Veteran
Veteran


Joined: 20 Mar 2006
Posts: 1275
Location: Tri-Cities, WA USA

PostPosted: Sat Feb 20, 2016 5:07 pm    Post subject: Reply with quote

some how things are now working:
#crontab -e
*/5 * * * * /bin/bash -l /home/cwc/bash/makelogs.s

/5 works better.

Here is my script:

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


I am not 100% sure what bash -l does though.
_________________
Without diversity there can be no evolution:)
Back to top
View user's profile Send private message
cwc
Veteran
Veteran


Joined: 20 Mar 2006
Posts: 1275
Location: Tri-Cities, WA USA

PostPosted: Sat Feb 20, 2016 5:28 pm    Post subject: Reply with quote

I don't understand this:
/bin/bash -l /home/cwc/bash/makelogs.sh

From man bash

-l Make bash act as if it had been invoked as a login shell (see INVOCATION below).

A login shell is one whose first character of argument zero is a -, or one started with the --login option.


I'd appreciate an explanation.
Thanks for all the help.
_________________
Without diversity there can be no evolution:)
Back to top
View user's profile Send private message
szatox
Advocate
Advocate


Joined: 27 Aug 2013
Posts: 3117

PostPosted: Sat Feb 20, 2016 8:22 pm    Post subject: Reply with quote

Quote:
I don't understand this:
/bin/bash -l /home/cwc/bash/makelogs.sh
So ugly... You launch bash for login and then launch bash again inside the script to execute the commands. You can pass parameters in the #! line. Handy thing (imagine '#! /bin/sed -f' for text-processing scripts)
Anyway, see the difference below:

Code:
 # cat test.sh
#! /bin/bash
echo '### normal shell launched by cron ###'
whoami
env
exec /bin/bash -l << EOF
echo '### login shell launched by cron ###'
whoami
env
EOF

Code:

# cat test.log
### normal shell launched by cron ###
root
SHELL=/bin/sh
USER=root
PATH=/usr/bin:/bin
PWD=/root
HOME=/root
SHLVL=2
LOGNAME=root
_=/usr/bin/env
### login shell launched by cron ###
root
MANPATH=/etc/java-config-2/current-system-vm/man:/usr/local/share/man:/usr/share/man:/usr/share/gcc-data/x86_64-pc-linux-gnu/4.9.3/man:/usr/share/binutils-data/x86_64-pc-linux-gnu/2.25.1/man:/etc/java-config-2/current-system-vm/man/:/usr/lib64/erlang/man
SHELL=/bin/sh
ANT_HOME=/usr/share/ant
USER=root
        ~~/ SNIP /~~
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/bin:/usr/x86_64-pc-linux-gnu/gcc-bin/4.9.3
INFOPATH=/usr/share/info:/usr/share/gcc-data/x86_64-pc-linux-gnu/4.9.3/info:/usr/share/binutils-data/x86_64-pc-linux-gnu/2.25.1/info
        ~~/ SNIP /~~
_=/usr/bin/env
Back to top
View user's profile Send private message
cwc
Veteran
Veteran


Joined: 20 Mar 2006
Posts: 1275
Location: Tri-Cities, WA USA

PostPosted: Sat Feb 20, 2016 11:11 pm    Post subject: Reply with quote

that is epic szatox

Thanks.
Now another question?
Why the EOF

Code:

#! /bin/bash
echo '### normal shell launched by cron ###'
whoami
env
exec /bin/bash -l << EOF
echo '### login shell launched by cron ###'
whoami
env
EOF


_________________
Without diversity there can be no evolution:)


Last edited by cwc on Sun Feb 21, 2016 3:48 am; edited 1 time in total
Back to top
View user's profile Send private message
szatox
Advocate
Advocate


Joined: 27 Aug 2013
Posts: 3117

PostPosted: Sat Feb 20, 2016 11:36 pm    Post subject: Reply with quote

Redirect uses it.
A few tricks with redirecting standard input:
Code:
command < name_of_an_input_file

command << END_TAG
multi
line
input
END_TAG

command <<< "string"

command <( process substitution - run this command in bracket first and use its output )

command1 | command2 # the old, good, well known pipe. Mind that it's not equal to the above: the one in brackets runs sequentially.
# command2 runs in parallel, but it also runs in a subshell.
# This affects propagation of variables!
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Networking & Security All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum