bash 从 crontab 运行时找不到 Sqlplus 命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41302304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sqlplus command not found when running from crontab
提问by Robenya
This is the case, I made 3 files to execute backup database command in rman
是这样的,我在rman中做了3个文件执行备份数据库命令
test.sh:
测试.sh:
#!/bin/bash
sqlplus /nolog @/u01/conectar.sql
conectar.sql:
conectar.sql:
connect sys/manager as sysdba
ho rman target mydatabase/mypassword @/u01/backup.sh
backup.sh:
备份.sh:
#!/bin/bash
RUN {backup database;}
and then I did all the chmod u+x
for the files to make them executable, then export EDITOR=nano
to change the cron editor.
然后我chmod u+x
为这些文件做了所有的工作以使它们可执行,然后export EDITOR=nano
更改 cron 编辑器。
when I go to crontab -e i put
当我去 crontab -e i put
00 15 * * * /u01/test.sh
If I clic this test.sh manually, the operation runs normally, but then in the crontab I get the "you got a mail" thing with this message
如果我手动单击此 test.sh,则操作会正常运行,但是在 crontab 中,我会收到带有此消息的“您收到一封邮件”的消息
From [email protected] Thu Dec 22 16:20:01 2016 Return-Path: X-Original-To: oracle Delivered-To: [email protected] Received: by localhost.localdomain (Postfix, from userid 500) id 956CD41D4B; Thu, 22 Dec 2016 16:20:01 -0400 (AST) From: [email protected] (Cron Daemon) To: [email protected] Subject: Cron /u01/test.sh Content-Type: text/plain; charset=UTF-8 Auto-Submitted: auto-generated X-Cron-Env: X-Cron-Env: X-Cron-Env: X-Cron-Env: X-Cron-Env: Message-Id: <[email protected]> Date: Thu, 22 Dec 2016 16:20:01 -0400 (AST) /u01/test.sh: line 3: sqlplus: command not found"
来自 [email protected] Thu Dec 22 16:20:01 2016 返回路径:X-Original-To:oracle Delivered-To:[email protected] Received:通过 localhost.localdomain(后缀,来自用户 ID 500)id 956CD41D4B ; 2016 年 12 月 22 日星期四 16:20:01 -0400 (AST) 从:[email protected](Cron Daemon)到:[email protected] 主题:Cron /u01/test.sh 内容类型:text/plain;charset=UTF-8 自动提交:自动生成的 X-Cron-Env:X-Cron-Env:X-Cron-Env:X-Cron-Env:X-Cron-Env:Message-Id:<20161222202001.956CD41D4B@ localhost.localdomain> 日期:2016 年 12 月 22 日星期四 16:20:01 -0400 (AST) /u01/test.sh: line 3: sqlplus: command not found"
Please can you remake the script or the crontab for me? If you can answer with the exactly modifications I would appreciate it, I'm not an expert in this environment so a general knowledge neededanswer will leave me the same, thanks.
请问你能帮我重新制作脚本或crontab吗?如果您能以准确的修改回答我将不胜感激,我不是这种环境的专家,因此需要的一般知识答案将使我保持不变,谢谢。
回答by codeforester
You can do it in many ways:
您可以通过多种方式做到这一点:
Just before the cron line:
PATH=$PATH:/full/path/to/oracle/bin
Or on the cron line itself:
00 15 * * * PATH=$PATH:/full/path/to/oracle/bin /u01/test.sh
Let your script
test.sh
source another shared script that sets up your Oracle environment:source /path/to/oracle_env.sh
就在 cron 行之前:
PATH=$PATH:/full/path/to/oracle/bin
或者在 cron 线上:
00 15 * * * PATH=$PATH:/full/path/to/oracle/bin /u01/test.sh
让您的脚本获取
test.sh
另一个用于设置 Oracle 环境的共享脚本:source /path/to/oracle_env.sh
I prefer the third method because it is very flexible and it helps us keep the crontab uncluttered. .bash_profile should be meant for interactive shell only - it is not good to share it with scheduled scripts, especially in production.
我更喜欢第三种方法,因为它非常灵活,可以帮助我们保持 crontab 整洁。.bash_profile 应该仅用于交互式 shell - 与预定脚本共享它是不好的,尤其是在生产中。