1

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

Zhilong Jia
  • 2,329
  • 1
  • 22
  • 34
  • Does not work. Thank you. @Inian – Zhilong Jia Sep 14 '17 at 13:03
  • do `which ack`, then take that value and use it in your call inside your script. That is to say, use the `/full/path/to/ack` in your script. Else add that value to the `PATH` variable. This sort of Q gets asked everyday here, please learn to use the search feature. Good luck. – shellter Sep 14 '17 at 13:16
  • @shellter, It does not work. your answer is the same as Inian's. Thank you. – Zhilong Jia Sep 14 '17 at 13:21
  • "cannot work" So the files `/ext/1(2).t` are empty? Is `/ext` something besides a standard disk-drive file system? What happens if you replace `/ext` with `/tmp` ? Hmm... `Linux timemachine` Is that your computers, or are you running this on a firmwared product like a NAS? Did you look at `man ack`, is there anything about no output when not being run from a terminal device? Good luck. – shellter Sep 14 '17 at 16:07
  • didn't know about `ack`. Very interesting read on the man page. Do you have a `.ackrc` file, and if so does your crontab user have the same file? Maybe change your script to use `--noenv` ? Otherwise, there is a mail group for ack, so maybe post your problem there. What you are describing is out of the realm for traditional *nix programs. Retracting Close vote and ++ as this is not your usual crontab problem. Good luck! – shellter Sep 14 '17 at 16:25
  • @shellter "cannot work" So the files /ext/1(2).t are empty? (Only /ext/1.t is empty.); if you replace /ext with /tmp (the same problem.); are you running this on a firmwared product like a NAS? (No.); `--noenv` has no effect on this. I will reply once I get a solution from other sources. Thank you. – Zhilong Jia Sep 15 '17 at 00:41

2 Answers2

1

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.

gbtimmon
  • 4,238
  • 1
  • 21
  • 36
  • Nothing is changed using `/usr/bin/ack "localhost" /etc/hosts 2>&1 > /ext/1.t` . Thank you. Have you tested in your Linux? BTW, if there are errors, cron will send a mail. However, none email was received. – Zhilong Jia Sep 14 '17 at 13:34
  • isn't `... > log 2>&1` the usual formulation? It used to make a difference ;-) . Otherwise, and very patient explanation of stream redirections! Good luck to all. – shellter Sep 14 '17 at 13:38
  • Does not work, still @shelter. Exactly, stream redirections are clearly explained. Thank you. – Zhilong Jia Sep 14 '17 at 13:44
0

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

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

Zhilong Jia
  • 2,329
  • 1
  • 22
  • 34