如何使Exp/Expdp Oracle CLI与Linux中的Cronjob一起使用?
我在/etc/crontab linux文件中定义了以下cronjob:
* * * * * oracle cd "/home/oracle" && exp "user"/"password"@"TNS" owner="user" file="user".dmp log="user".log;
为了示例性,在所有字段中都使用Asterisk Symbol *来代表任何时间exp
命令执行。
当我尝试直接通过CLI执行此命令时,它的工作正常很好,即,备份和日志文件是在执行该命令的目录中创建的:
cd "/home/oracle" && exp "user"/"password"@"TNS" owner="user" file="user".dmp log="user".log
但是,出于某种原因,当Cron试图执行它时,它不起作用。为什么?我已经尝试定义path
和shell
在文件顶部的变量,以便cron环境可以看到在哪里可以找到此exp
可执行。就像这样:
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin:$ORACLE_HOME/bin
* * * * * oracle cd "/home/oracle" && exp "user"/"password"@"TNS" owner="user" file="user".dmp log="user".log;
我也尝试将$ oracle_home替换为其文字目录,即/opt/opt/oracle
;它不起作用。
我要么尝试将exp
命令替换为根据其绝对路径所在的位置,就像这样:/opt/opt/oracle/bin/exp
;但是它行不通。
不过,Cron正在工作,因为当我执行更简单的任务时(例如,在CD命令将CRON输入的目录中创建一个文件时),它可以正常工作。
* * * * * cd "/home/oracle" && touch text.txt;
同样,这为什么会发生?我是否缺少有关Cron的工作方式的一些相关信息?
I have the following cronjob defined in my /etc/crontab
Linux file:
* * * * * oracle cd "/home/oracle" && exp "user"/"password"@"TNS" owner="user" file="user".dmp log="user".log;
For purposes of exemplification, the asterisk symbol *
is being used in all fields to represent any time for the exp
command to execute.
When I try to execute this command directly via CLI, it works perfectly fine, that is, the backup and log files are created at the directory where the command is being executed in:
cd "/home/oracle" && exp "user"/"password"@"TNS" owner="user" file="user".dmp log="user".log
However, for some reason, when cron tries to execute it, it does not work. Why? I have already tried to define both PATH
and SHELL
variables at the top of the file, so that cron environment could see where to find this exp
executable. Just like this:
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin:$ORACLE_HOME/bin
* * * * * oracle cd "/home/oracle" && exp "user"/"password"@"TNS" owner="user" file="user".dmp log="user".log;
I tried to replace the $ORACLE_HOME to its literal directory too, which is /opt/oracle
; it didn't work.
I either tried to replace the exp
command to where it is located according to its absolute path, just like this: /opt/oracle/bin/exp
; it didn't work nevertheless.
Cron is working though, because when I execute a simpler task (for instance, create a file in a directory where cd command took the cron into), it works fine.
* * * * * cd "/home/oracle" && touch text.txt;
Again, why is this happening? Am I missing some relevant information about how cron works?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
写一个外壳脚本以执行您的导出。在该脚本中定义环境变量;您不能在crontab中做到这一点。测试您的Shell脚本独立工作,然后使用CRON运行Shell脚本。
/home/oracle/export.sh:crontab
:
另外:
Write a shell script to perform your export. Define your environment variables within that script; you can't do it in the crontab. Test that your shell script works independently, then use cron to run the shell script.
/home/oracle/export.sh:
crontab:
Also:
CRON提交的过程不会像登录到命令行shell进程(例如SSH)一样执行.bash_profile脚本。因此,您的cron作业并不是继承您的命令行过程的所有环境。
您不应该在crontab条目中将一堆命令堆叠在一个“命令行”上,而应编写一个执行您想要/需要的所有内容的shell脚本 - 包括呼叫oraenv以设置环境:
script my_expdp.sh
,然后在crontab-
编辑
我看到@pmdba发布的解决方案基本上与我写了答案。
- 编辑II
修改以允许用于多个数据库的单个脚本。在脚本中,而不是硬编码Oracle_sid的值,而是将其设置为第一个命令行参数的值,在调用脚本时设置。然后,在crontab条目(无论如何都必须为每个作业编码),为Oracle_SID设置值。在上面的示例中,Oracle_SID将设置为“ myDB”。
processes submitted by cron do not get the .bash_profile script executed, as does on when one logs on to a command-line shell process such as ssh. Thus, your cron job is not inheriting all of the environment that your command line process has.
Rather than try to stack a bunch of commands onto one 'command line' in the crontab entry, you should write a shell script that does everything you want/need - including a call to oraenv to set the environment:
script my_expdp.sh
Then, in crontab
-- Edit
I see that @pmdba posted essentially the same solution as I did, even as I was writing my answer.
-- Edit II
Modifying to allow a single script to be used against multiple databases. In the script, instead of hard-coding the value of ORACLE_SID, set it to the value of the first command line parameter, set when calling the script. Then, on the crontab entry (which will have to be hard-coded for each job anyway), set the value for ORACLE_SID. In example above, ORACLE_SID will be set to 'mydb'.