脚本内容:

#!/usr/bin/expect
set database [lindex $argv 0]
set username [lindex $argv 1]
set password [lindex $argv 2]
set command  [lindex $argv 3]
spawn  psql -U $username -d $database -c "$command"  -W
expect "Password: "
send "$password\n"
expect eof

postgresql 默认的命令行是需要交互的,并不能实现在crontab中自动执行一个sql语句之类的操作,借助expect就可以实现这个功能(如果没有except,需要先安装expect)

使用方法(假设将上边的脚本保存为runcommand.sh):

./runcommand.sh dbname username password command

相关文章:

  • 2022-01-05
  • 2022-12-23
  • 2021-11-07
  • 2022-12-23
  • 2021-05-17
  • 2021-05-07
  • 2022-12-23
  • 2021-12-04
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-16
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案