crontab 在unix 没有执行。

 

Quote: 引用 2 楼 jdwq33 的回复:

Quote: 引用 1 楼 mp777323 的回复:

03 * * * * sh /opt/pro_some.sh
我试过了,这样也不行,难道是我的系统出问题了吗?

看下邮件, /var/spool/mail/root
会有执行报告的


那个root文件里面没有打印任何东西,我等了这么久,一点东西都没有。

 

 

系统任务调度:系统周期性所要执行的工作,比如写缓存数据到硬盘、日志清理等。在/etc目录下有一个crontab文件,这个就是系统任务调度的配置文件。

/etc/crontab文件包括下面几行:

[root@localhost ~]# cat /etc/crontab 

SHELL=/bin/bash

PATH=/sbin:/bin:/usr/sbin:/usr/bin

MAILTO=""HOME=/

 

# run-parts

51 * * * * root run-parts /etc/cron.hourly

24 7 * * * root run-parts /etc/cron.daily

22 4 * * 0 root run-parts /etc/cron.weekly

42 4 1 * * root run-parts /etc/cron.monthly

[root@localhost ~]#

 

前四行是用来配置crond任务运行的环境变量,第一行SHELL变量指定了系统要使用哪个shell,这里是bash,第二行PATH变量指定了系统执行命令的路径,第三行MAILTO变量指定了crond的任务执行信息将通过电子邮件发送给root用户,如果MAILTO变量的值为空,则表示不发送任务执行信息给用户,第四行的HOME变量指定了在执行命令或者脚本时使用的主目录。第六至九行表示的含义将在下个小节详细讲述。这里不在多说。

 

 

 

unix issu1 :

 使用命令:mail 也可以查看


检查cron失败运行报错:
cat /var/spool/mail/root

检查cron成功运行:
cat /var/log/cron

 

OSW 没有执行,检查 /var/spool/mail/root 文件,发现cron 模式是从根目录开始执行,因此无法发现执行目录。

 

sh修改如下:

  $SCRIPTLOC/OSWatcher.sh 30 360

修改为

cd $SCRIPTLOC
$SCRIPTLOC/OSWatcher.sh 30 360

 

 另外加入profile 选项:

cd $HOME
OSNAME=`uname`
case $OSNAME in
SunOS) OSNAME=1 ;;
HP-UX) OSNAME=2 ;;
AIX) OSNAME=3 ;;
Linux) OSNAME=4 ;;
esac
if [ $OSNAME -eq 4 ]
then
. ./.bash_profile
else
. ./.profile
fi

 

 

linux issue 2:

 

crontab执行后日志文件路径

/var/log/cron

如crontab 没有成功,检测crontab 服务是否启动,

/etc/init.d/crond status 

/etc/init.d/crond restart

cron 是系统守护进程,位置:
/etc/init.d/crond
参数:
start
stop
restart
reload

 

 

crontab执行后日志文件路径

/var/log/cron

 

 

##issue 3

感谢

Linux定时任务,执行shell文件失败问题&&mailed 73 bytes of output but got status 0x004b#012报错解决

加入调试语句   2>&1


为了找到错误的具体信息,我把语句改成了

mysqldump -uxxx -pxxx dbname > backup.sql >xxx.log 2>&1

###########sample :

31 16 * * * sh /orabak/oswbb/oswbb/OSW_init.sh > /tmp/1.log

 

unix AIX: 使用cron fork 出一个OSW进程

[root@nbutest1:/]# ps -ef|grep OSW
root 11862018 19202296 0 Nov 30 - 0:32 /bin/sh ./OSWatcherFM.sh 360 /oradata/oswbb/oswbb/archive 《— 第三级子进程
root 12910804 21889042 0 16:02:00 pts/5 0:00 grep OSW
root 19202296 29818988 0 Nov 30 - 1:14 /bin/sh /oradata/oswbb/oswbb/OSWatcher.sh 30 360 <- 第二级子进程
root 29818988 6815964 0 Nov 30 - 0:00 sh /oradata/oswbb/oswbb/OSW_init.sh <- 第一级子进程
[root@nbutest1:/]# ps -ef|grep 6815964
root 6815964 1 0 Sep 15 - 0:53 /usr/sbin/cron <- 这是父进程,父进程是cron
root 29818988 6815964 0 Nov 30 - 0:00 sh /oradata/oswbb/oswbb/OSW_init.sh
root 32833782 21889042 0 16:03:07 pts/5 0:00 grep 6815964

 

linux: 使用cron fork 出3个OWS进程


[root@nbutest:/orabak/oswbb/oswbb]# more /tmp/2.log
root 10127 10126 0 16:26 ? 00:00:00 /bin/sh -c sh /orabak/oswbb/oswbb/OSW_init.sh > /tmp/1.log <- 第一级子进程
root 10128 10127 0 16:26 ? 00:00:00 sh /orabak/oswbb/oswbb/OSW_init.sh <- 第二级子进程
root 10136 10128 0 16:26 ? 00:00:00 sh /orabak/oswbb/oswbb/OSW_init.sh <- 第三级子进程


[root@nbutest:/orabak/oswbb/oswbb]# ps -ef|grep 10126
root 10126 5076 0 16:26 ? 00:00:00 crond <- 这是父进程,父进程是cron

 


unix HP-UNIX 使用cron fork 出2个OSW进程

[root@nbutest2:/]# more /tmp/2.log
root 20751 6990 2 16:31:00 ? 0:00 sh -c sh /orabak/oswbb/oswbb/OSW_init.sh > /tmp/1.log
root 20752 20751 0 16:31:00 ? 0:00 sh /orabak/oswbb/oswbb/OSW_init.sh

 

 

###issue 5

感谢

cx_Oracle库导入失败引起crontab中python程序运行失败,并且无错误提示

今天遇到一个问题:

       一个python脚本命令行运行时很正常,放到crontab中就无法工作,日志也没有记录,找了半天,终于发现问题所在。

在脚本最上方,程序如下:

  1.  
    #!/usr/local/bin python
  2.  
    # coding=utf8
  3.  
    import cx_Oracle
  4.  
    import sys
  5.  
    import time

注意,这里

import cx_Oracle  

cx_Oracle需要依赖oracle的ORACLE_HOME环境。我发现ORACLE_HOME定义在/etc/profile下,在这里修改的内容是对所有用户起作用的,但是对crontab是不起作用的。以下是各个文件的一个作用域:

 

(1)/etc/profile: 此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行. 并从/etc/profile.d目录的配置文件中搜集shell的设置。
(2)/etc/bashrc: 为每一个运行bash shell的用户执行此文件.当bash shell被打开时,该文件被读取。
(3)~/.bash_profile: 每个用户都可使用该文件输入专用于自己使用的shell信息,当用户登录时,该文件仅仅执行一次!默认情况下,他设置一些环境变量,执行用户的.bashrc文件。
(4)~/.bashrc: 该文件包含专用于你的bash shell的bash信息,当登录时以及每次打开新的shell时,该该文件被读取。
(5)~/.bash_logout: 当每次退出系统(退出bash shell)时,执行该文件. 另外,/etc/profile中设定的变量(全局)的可以作用于任何用户,而~/.bashrc等中设定的变量(局部)只能继承 /etc/profile中的变量,他们是"父子"关系。

(6)~/.bash_profile 是交互式、login 方式进入 bash 运行的~/.bashrc 是交互式 non-login 方式进入 bash 运行的通常二者设置大致相同,所以通常前者会调用后者。

解决办法:

30 7 * * *  . /etc/profile; /usr/local/bin/python2.7 /data/storeDataToOracleDB.py  &  

这里加上了

. /etc/profile;  

ok,问题解决。

 

 

#######sample 3


检查cron失败运行报错:
cat /var/spool/mail/root

检查cron成功运行:
cat /var/log/cron

 


感谢 Lukasz Stelmach
https://stackoverflow.com/questions/3241086/how-to-schedule-to-run-first-sunday-of-every-month

检查cron失败运行报错:
cat /var/spool/mail/root

检查cron成功运行:
cat /var/log/cron
前2周周5 09:40 运行脚本 (但每月日期小于17号的每周5 运行该脚本)
40 09 * * 5 [ $(date +\%d) -le 17 ] && echo "1" >> /tmp/p.log

 


You can put something like this in the crontab file:

00 09 * * 7 [ $(date +\%d) -le 07 ] && /run/your/script
The date +%d gives you the number of the current day, and then you can check if the day is less than or equal to 7. If it is, run your command.

If you run this script only on Sundays, it should mean that it runs only on the first Sunday of the month.

Remember that in the crontab file, the formatting options for the date command should be escaped.

 

##issue 4

mail 提示 crontab  报错 /bin/sh: 22: 未找到命令

 

fix:

1.shell 里加入

  . /etc/profile

 

2. crontba -l 最括号丢失

 $(date +\%d) -le 22 ] &&  hostname > /tmp/host

应该为

[ $(date +\%d) -le 22 ] &&  hostname > /tmp/host

 

 

######issue 5

 

https://blog.csdn.net/cuma2369/article/details/107667987

 感谢

crontab -e启动编辑器来编辑crontab条目。 但是如何在Linux上的shell脚本中添加crontab条目而又无需用户交互?

You can try this piece of script:

您可以尝试以下脚本:

(crontab -l; echo "@reboot echo "rebooted"";) | crontab -

Note that the update by this line of script is not atomic. If some other programs edit the crontab files between the first and second invokes of crontab, the edits will be lost. Hence, make sure there is not other programs/admins editing the crontab when you use this piece of script.

 

 

####sample 5

https://blog.51cto.com/net881004/2374589

Linux下用crontab执行定时任务的时候不会读取全局环境变量和当前用户环境变量(/etc/profile、~/.bash_profile),所以会导致手工执行某个脚本时是成功的,但是crontab定期执行有问题。

 

两种解决办法:

1、在Shell文件里面获取环境变量值的路径写成绝对路径,别用环境变量的路径值。

比如mysql替换成/usr/local/mysql/bin/mysql
  • 1.
 
 

 

2、Shell脚本缺省的#!/bin/bash开头换行

#!/bin/bash
. /etc/profile
. ~/.bash_profile
  • 1.
  • 2.
  • 3.
 
 

 

 

 

posted @ 2017-11-23 11:58  feiyun8616  阅读(600)  评论(0编辑  收藏  举报