Script Produces Different Result When Executed by Bash Than by Cron

Script produces different result when executed by Bash than by cron

As indicated in the comments, do use full paths on crontab scripts, because crontab does have different environment variables than the normal user (root in this case).

In your case, instead of mdadm, /sbin/mdadm makes it.

How to get the full path of a command? Using the command command -v:

$ command -v rm
/bin/rm

different results between crontab and running script manually

First of all, there are a couple of problems in your grep regular expression:

  1. The repetition count ({1,3}) applies to its preceding atom (i.e. '.') rather than the next one (i.e. '[0-9]').
  2. An unescaped dot ('.') in the regex matches any character, which hides the previous error. Your regex (192.168.0.{1,3}[0-9]) matches 192.168.0.123 as follows:

    192.168.0 matches 192.168.0
    .{1,3} matches .12
    [0-9] matches 3

    But it would also match the following strings:

    192116810abc1
    192.681.012.9

The correct regex must be 192\.168\.0\.[0-9]{1,3} and it must be quoted, so that bash passes it to grep literally:

grep -Eo '192\.168\.0\.[0-9]{1,3}'

Yet, the wrong regex can hardly explain the problem your are seeing with cron.

One problem may be that you are using a fixed name new.txt for your temporary file. If you do the same in your other scripts, or if you set up this cron job to run every minute while it takes nmap more than a minute to complete scanning the network, then new.txt may be overwritten at the wrong time.

Please fix your script as follows and check if the problem disappears:

#!/bin/bash

tmpfile="$(mktemp)"
trap "rm $tmpfile" EXIT
nmap -sn 192.168.0.1-255 | grep -Eo '192\.168\.0\.[0-9]{1,3}' > "$tmpfile"
date >> network_log
echo ---------------------------- >> network_log
cat "$tmpfile" >> network_log

Bash script returns different value when run from cron job than from command line

Try to add

source ~/.bashrc

after the shebang, and another important thing :

don't call the script with sh if it's bash !

With sh, bash is in POSIX mode. I suspect that's not intended here.

Different results when running a script by cron than manually executed

This is a guess, but I suspect it's due to using more to read the file -- more is an interactive tool, and shouldn't be used in non-interactive scripts. Using cat would be better, but a while read ... done <file loop is generally the best way to do this. And rather than using a messy set of intermediate files, I'd just use a single combined while read loop to read both files in parallel (via file descriptors #3 and #4, just to keep things clean), and put the info from the two files together directly:

#!/bin/bash
PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/puppetlabs/bin
temp=/local/scripts/shell/checker/temp
timestamp=$(date +'%Y_%m_%d--%H:%M:%S')

while read domain <&3 && read key <&4; do
crl="https://${domain}//api/?type=op&cmd=blablabla&key=${key}"
filename="${temp}/${domain}_show_counters.txt"
curl -k "$crl" > "${filename}"
done 3<"${temp}/list.txt" 4<"${temp}/keys.txt"

Note: I also cleaned up the quoting (in general, variable references should be in double-quotes), and left the trailing / off of the temp variable (I find it much clearer to add it when using the variable -- makes it clear we're referring to a file in that directory).

Running R script from crontab gives different results than from command line

Don't compare a string to a number if you don't have to. If you removed the space from the format pattern (i.e. made it "%H" instead of " %H"), it should work. However, you don't have to introduce strings in this case; you can get the hour as a number by converting to POSIXlt and extracting the hour component. Try this:

as.POSIXlt(Sys.time())$hour > 12

Python cron job returns different output than manual execution

The problem was to do with the top command output being truncated to 80 characters when run via cron as answered here: https://unix.stackexchange.com/questions/95877/output-of-top-gets-truncated-to-80-columns-when-run-by-cron

Changing my cron job to include the COLUMNS option fixed the problem

Original

# Run monitor script every 15 minutes
*/15 * * * * /mnt/active/monitor_cluster/monitor.py

Working

# Run monitor script every 15 minutes
*/15 * * * * COLUMNS=200 /mnt/active/monitor_cluster/monitor.py



Related Topics



Leave a reply



Submit