ack outputs nothing using crontab in linux

136 views Asked by At

ack outputs nothing using crontab in linux.

In the crontab file (edited with sudo crontab -e):

39 20 * * * /ext/test110.sh

And cat /ext/test110.sh will show

#! /bin/sh

/usr/bin/ack "localhost" /etc/hosts > /ext/1.t
which ack > /ext/2.t

After cron, there are 1.t and 2.t in /ext

cat 2.t will output /usr/bin/ack; However, nothing in 1.t.

If I run ack "localhost" /etc/hosts > /ext/3.t in bash (4.3.30) or sh directly, it will output: 127.0.0.1 localhost

It seems ack cannot work with cron. where is the bug? Thank you.

uname -a:

Linux xxx 3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt25-2 (2016-04-08) x86_64 GNU/Linux

ack 2.14, Running under Perl 5.20.2 at /usr/bin/perl

2

There are 2 answers

0
Zhilong Jia On BEST ANSWER

/usr/bin/ack --nofilter "localhost" /etc/hosts > /ext/1.t works.

Long Answer see https://github.com/beyondgrep/ack2/issues/649

3
gbtimmon On

Ack is probably throwing an error but you are not seeing in since you are only piping the stdout and error gets written to std err.

As a rule of thumb always grab the stderr in a cron log unless you have a good reason not to.

Redirect a stream with the command:

 [stream-no]>[destination]

If no stream no is given bash will default to 1 which corresponds to stdout so:

>/file == 1>/file

You can do stderr by using the stderr file-no, 2. To send stderr to a file:

2>/file 

You can send a stream into another by using &file-no as the destination. You can send stderr to the same place as stdout with:

2>&1

So in you script try this. run ack, send the stderr to file-no 1 along with stdout and then send everything coming into file-no 1 into the log file.

/usr/bin/ack "localhost" /etc/hosts 2>&1 > /ext/1.t 

And check the output. I bet you get some text now, most likely an error which you can no intelligently fix.