# crontab not working ? [solved]

## cwc

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;

----------

## Syl20

Is cron daemon started ?

Off-topic, but :

 *cwc wrote:*   

> * * * * * /bin/bash /home/cwc/bash/logfile.sh
> 
> (...)
> 
> #!/bin/sh

 

----------

## cwc

 *CneGroumF wrote:*   

> Is cron daemon started ?
> 
> Off-topic, but :
> 
>  *cwc wrote:*   * * * * * /bin/bash /home/cwc/bash/logfile.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?

----------

## szatox

Is the script executable?

Is $PATH provided for your script? (try #! /bin/bash -l )

----------

## cwc

 *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.

----------

## gordonb3

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.

----------

## Syl20

 *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.

```
# 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 :

```
# equery l '*cron*'
```

----------

## cwc

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:

```

# 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 ~ # 

```

----------

## cwc

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.

----------

## cwc

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.

----------

## szatox

 *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:

```
 # 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
```

```

# 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

```

----------

## cwc

that is epic szatox

Thanks.

Now another question?

Why the EOF

```

#! /bin/bash 

echo '### normal shell launched by cron ###' 

whoami 

env 

exec /bin/bash -l << EOF 

echo '### login shell launched by cron ###' 

whoami 

env 

EOF

```

----------

## szatox

Redirect uses it.

A few tricks with redirecting standard input:

```
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!
```

----------

