Crontab Timing task fails, run a separate shell script feasible

Exclude ideas

1. Make sure that the boot from Kai crond service is started and is currently

# centos 7
systemctl status crond
systemctl start crond

# centos 5,6
service crond  status 
chkconfig crond on

 2. Check the mail error messages inside information about the scheduled task.

mail

Borrowing the network over the upper diagram,

 

 

In this case, the general configuration of these environments in the profile file, may be the next source in a script file, let it take effect, or the corresponding configuration script written to come inside.

source /etc/profile

3. The script inside the program is not in the PATH command inside. 

Inside the command script execution, not in the environment variable plan tasks performed there, you need to add a command to write the whole path or paths.

# Get all PATH in the terminal can perform, write a script
  echo $ PATH 
/ usr / local / sbin: / usr / local / bin: / usr / sbin: / usr / bin: / usr / local / PHP / bin : / usr / local / Docker / bin:. / usr / local / the Java / jdk1 8.0_191 / bin:. / usr / local / the Java / jdk1 8.0_191 / jre / bin: / usr / local / Flume / bin: / usr / local / Go / bin: / usr / local / Go / bin: / root / bin # add the following lines similar to PATH = / usr / local / bin in the implementation of the script: $ PATH

4. Alternatively bash

Plans to use crontab -e to edit configuration line, the command is limited, redirection and special symbols are not supported, these are offered bash, you can use the following way configuration.

# add by zhaojiedi 2019.08.30 
14 * * * * bash /usr/local/bin/backup_k8s.sh

 5. Under the planned task script line package

We plan to task row, and the write command if a long date with a few commands, suggested that these commands written to a new file to go inside, scheduled tasks command line to write the new file as a command.

# Originally planned task row 
* * * * cd / Home / Barry / top800 / TOP10 / top10_fruits / && ./top10_all. SH 

# rewritten echo " cd / Home / Barry / top800 / TOP10 / top10_fruits / && ./top10_all. SH "> / usr / local / bin / TOP10. SH * * * * * bash / usr / local / bin / TOP10. SH

And finally put a complete scheduled tasks script points

#!/bin/bash
source /etc/profile
PATH=/usr/local/bin:$PATH
base_dir=/alidata/k8s_backup
dt=$(date "+%F/%T")
cmd_history_file="/usr/local/limikeji/bin/cmd_history.txt"

#  先处理有命名空间的
kubectl  api-resources   |grep true  |awk 'BEGIN {OFS=" "; FIELDWIDTHS="34 13 31 13 50"}{if (NR >1)print $5,$1,$4,$3}' |while read kind name has_namespace group ; do 
        #echo "$kind $name $has_namespace $group"
        api_path="$kind"
        cmd="kubectl  get $api_path --all-namespaces -ocustom-columns=name:.metadata.name,namespace:.metadata.namespace"
        $cmd | awk '{if(NR>1)print $0}' |while read  record_name  ns ; do
                file="$base_dir/$dt/$ns/$api_path/${record_name}.yml"
                mkdir -pv $(dirname $file)
                cmd="kubectl  -n  $ns get  $api_path/$record_name -oyaml "
                echo $cmd >>$dt_$cmd_history_file
                $cmd > $file
        done
done

#  后处理无命名空间的
kubectl  api-resources   |grep false  |awk 'BEGIN {OFS=" "; FIELDWIDTHS="34 13 31 13 50"}{if (NR >1)print $5,$1,$4,$3}' |while read kind name has_namespace group ; do 
        #echo "$kind $name $has_namespace $group"
        api_path="$kind"
        cmd="kubectl  get $api_path  -ocustom-columns=name:.metadata.name"
        $cmd | awk '{if(NR>1)print $0}' |while read  record_name   ; do
                file="$base_dir/$dt/NONENAMESPACE/$api_path/${record_name}.yml"
                mkdir -pv $(dirname $file)
                cmd="kubectl  get  $api_path/$record_name -oyaml "
                echo $cmd >>$dt_$cmd_history_file
                $cmd > $file
        done
done

 

 

 

Guess you like

Origin www.cnblogs.com/tanxiaojun/p/12006968.html