免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 3282 | 回复: 12
打印 上一主题 下一主题

手动执行shell没问题,改成用crontab定时执行就有问题,为什么?请指点! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-07-05 18:26 |只看该作者 |倒序浏览
----------
本人写了一个shell,shell文件名为execbat.sh,保存路径为:/export/home/stsuser
sehll文件内容如下:
-------------------------------------------
#!/bin/sh
cd /export/home/stsuser/nas/dts
./data_bat
------------------------------------------
shell的目的,我想先进入目录/export/home/stsuser/nas/dts
然后执行此目录下的程序data_bat

现在用手工执行,使用用户stsu,telnet登录系统,先进入目录/export/home/stsuser
直接运行shell文件./execbat.sh
data_bat程序可以可以正常执行,

现在改用crontab定时执行,使用用户stsu,telnet登录系统,crontab中加入如下任务:
25 18 * * * /export/home/stsuser/execbat.sh

说明一下,程序data_bat是pro*c程序,由data_bat.pc文件编译而成的可执行文件,
文件内容如下的形式:
------------------------------------------------------------------------------------------
#include <stdio.h>
#include <string.h>
.....
#include <sqlca.h>

main()
{
        char db_username[10];
        char db_passwd[10];
        memset(db_username,0,sizeof(db_username));
        memset(db_passwd,0,sizeof(db_passwd));
        
        /*读配置文件得到数据库用户名及密码*/
        ReadCfgFile("scfgfile.cfg","dbusername",db_username);
        ReadCfgFile("scfgfile.cfg","dbpaswd",db_passwd);
        
        EXEC SQL CONNECT :db_username IDENTIFIED by :db_passwd;
        if ( sqlca.sqlcode != 0 )
        {
                printf("connect database error. errcode[%d]",sqlca.sqlcode);
                return -1;
        }
        
        /*其他处理语句*/
        .......
        
        
        EXEC SQL COMMIT WORK RELEASE;
        return 0;
}
------------------------------------------------------------------------------------------
当用定时任务时,在连接数据库时,出现如下错误信息:
connect database error. errcode[-12545]

本人查询了错误:
ORA-12545: Connect failed because target host or object does not exist
Cause: The address specified is not valid, or the program being connected to does not exist.

看了错误,本人改了data_bat.pc代码,数据库连接处改成如下:
-------------------------------------------------------------------------------
EXEC SQL BEGIN DECLARE SECTION;
        VARCHAR  uid[200];
EXEC SQL END DECLARE SECTION;

main()
{
        char cnnstr[128];
        memset(cnnstr,0,sizeof(cnnstr));
        
        strcpy(cnnstr,"admin/nas4321@nasdat");
        strcpy ((char *)uid.arr,cnnstr);
        uid.len=(unsigned short)strlen((char *)uid.arr);
        EXEC SQL CONNECT :uid;
        if ( sqlca.sqlcode != 0 )
        {
                printf("connect database error. errcode[%d]",sqlca.sqlcode);
                return -1;
        }
        
        /*其他处理语句*/
        ............
        
-------------------------------------------------------------------------------
可用crontab定时执行还是有错:
connect database error. errcode[-12154]

本人百思不得其解,相同的用户,为何使用手工直接执行就没有任务问题,换成
crontab就会出问题,恳请高人指点,这是怎么了,如何解决?谢谢!

对了,系统为:
SunOS nas.sz.gmcc.net 5.8 Generic_117350-25 sun4u sparc SUNW,Sun-Fire
--

论坛徽章:
0
2 [报告]
发表于 2006-07-05 19:39 |只看该作者
check your user environment

sure the oracle parameter are declare in the shell file

论坛徽章:
0
3 [报告]
发表于 2006-07-05 19:40 |只看该作者
add env related variable or as root, su -l stsu -c xxx

论坛徽章:
0
4 [报告]
发表于 2006-07-05 20:14 |只看该作者
查看你的环境变量,一般是这些引起的故障!

论坛徽章:
0
5 [报告]
发表于 2006-07-06 09:20 |只看该作者
在cron及remsh的应用中都存在这样的问题,所以要在shell中加入所有的用户变量,才能确保你的程序可以正常运行

在用at的时候,系统会自动将用户的环境变量加入,而cron和remsh要自己加

论坛徽章:
0
6 [报告]
发表于 2006-07-06 10:33 |只看该作者
现改为:
25 18 * * * cd; ./.profile; /export/home/stsuser/execbat.sh
还是错误
晕!!!!

论坛徽章:
0
7 [报告]
发表于 2006-07-06 12:48 |只看该作者
there is easy way

cp .profile tempfile

more /export/home/stsuser/execbat.sh >> tempfile

cp tempfile /export/home/stsuser/execbat.sh

crontab -l
25 18 * * * /export/home/stsuser/execbat.sh

论坛徽章:
0
8 [报告]
发表于 2006-07-06 13:05 |只看该作者
注意你的文件的权限!

论坛徽章:
0
9 [报告]
发表于 2006-07-06 13:05 |只看该作者
在execbat.sh的第一行加上:
. $HOME/.profile

或者试这个
25 18 * * * cd; .  ./.profile; /export/home/stsuser/execbat.sh

论坛徽章:
0
10 [报告]
发表于 2006-07-06 13:44 |只看该作者
to:  doni   

25 18 * * * cd; .  ./.profile; /export/home/stsuser/execbat.sh
已经那样试过了,不行,还是错误
connect database error. errcode[-12545]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP