Top Command Output Is Empty When Run from Cron

Why does my command-line not run from cron?

It's often because you don't get the full environment when running under cron. Best bet is to capture the ouput by using the command:

( /sw/bin/perl /path/to/tv_grab_oztivo ... ) >/tmp/qq 2>&1

and then have a look at /tmp/qq.

If it does turn out to be a missing environment, then you may need to put:

. ~/.profile

or something similar, into the execution chain of your cron job, such as:

( . ~/.profile ; /sw/bin/perl /path/to/tv_grab_oztivo ... ) >/tmp/qq 2>&1

How to capture the output of a top command in a file in linux?

for me top -b > test.txt will store all output from top ok even if i break it with ctrl-c. I suggest you dump first, and then grep the resulting file.

I have a crontab entry that calls a bash script. Why does $USER return blank?

As suggested in the comments, cron uses a rather minimal environment, with most of the environment variables undefined, and a very short $PATH. In many versions of crontab, you can define the variables you need at the top of the crontab file, like so:

USER=foo
BAR=baz
PATH=/bletch:/bazoo:"$PATH"

CronJob not running

Finally I found the solution. Following is the solution:-

  1. Never use relative path in python scripts to be executed via crontab.
    I did something like this instead:-

    import os
    import sys
    import time, datetime

    CLASS_PATH = '/srv/www/live/mainapp/classes'
    SETTINGS_PATH = '/srv/www/live/foodtrade'
    sys.path.insert(0, CLASS_PATH)
    sys.path.insert(1,SETTINGS_PATH)

    import other_py_files
  2. Never supress the crontab code instead use mailserver and check the mail for the user. That gives clearer insights of what is going.

How to pass blank parameters to a script called in crontab (Linux)

I would suggest (without having tested it) the following:

* * * * * /usr/bin/env bash -c 'myscript.sh A B "" C D'

or alternatively just

* * * * * bash -c 'myscript.sh A B "" C D'

Captured output of command on remote host (SSH via Cron) is blank

  • First, tell SSH client to not to allocate a PTY with -T option, because obviously cron doesn't have one.
  • Then give it something infinite on stdin, so it will keep running until stdout
    is open, we have /dev/zero exactly for this purpose.

RAW_OUTPUT=$(timeout $TIMEOUT sshpass -e ssh -T -oStrictHostKeyChecking=no -oUserKnownHostsFile=/dev/null $USER@$HOST "$CMD" </dev/zero 2>/dev/null)

Netcat output to text file adds numerous ^@ characters when executed via Cron

Likely the environment is different when running under cron.

Try qualifying the exact path(s)

/usr/bin/nc -dl 12.34.56.78 1234 > /absolute/path/to/my/file & echo $!

Also note that there are two "competing" popular versions of netcat:

  • netcat-traditional
  • netcat-openbsd

with slightly different command line options

Also, consider just making things a loop:

#!/bin/sh
exec < /dev/null > /dev/null

while true
do
nc -dl 12.34.56.78 1234
done


Related Topics



Leave a reply



Submit