如何从另一个bash脚本中运行源文件并调用相应的函数,通过cron表。

huangapple go评论62阅读模式
英文:

How to run a bash script source from another bash script and invoke the corresponding functions. via crontab

问题

  • 打开终端后,我输入了这个命令:
moabdelaziz@pop-os:~$ crontab -r
  • 并且安排了这个任务:
* * * * * /bin/bash /home/moabdelaziz/Backups/backup.sh >> /home/moabdelaziz/output.txt
  • backup.sh
#!/bin/bash

TargetDir=/home/mohamed/Date
TargetBackup=/home/mohamed/Backup
EncryptionKey="PASS"
days=12

source ../backup_restore_lib.sh

validate_backup_params ${TargetDir} ${TargetBackup} ${EncryptionKey} ${days}
backup ${TargetDir} ${TargetBackup}
  • backup_restore_lib.sh 包含以下函数:
validate_backup_params {...}
Encryption {...}
Decryption {...}
remote_server {...}
backup {...}
validate_restore_params {...}
restore {...}

很遗憾,它不起作用 :(. 有人可以指出我做错了什么吗?提前感谢!

英文:
  • After I opened the terminal, I typed this command:
moabdelaziz@pop-os:\~$ crontab -r

  • And scheduled the task:
* * * * * /bin/bash /home/moabdelaziz/Backups/backup.sh >> /home/moabdelaziz/output.txt
  • backup.sh

#!/bin/bash

TargetDir=/home/mohamed/Date
TargetBackup=/home/mohamed/Backup
EncryptionKey="PASS"
days=12

source ../backup_restore_lib.sh

validate_backup_params ${TargetDir} ${TargetBackup} ${EncryptionKey} ${days}
backup ${TargetDir} ${TargetBackup}

  • backup_restore_lib.sh contains those functions

validate_backup_params {...}
Encryption {...}
Decryption {...}
remote_server {...}
backup {...}
validate_restore_params {...}
restore {...}

Unfortunately, it does not work :(.

Can anyone point me to anything I'm doing wrong? Thanks in advance!

答案1

得分: 1

一般来说,cron定时任务脚本在用户的主目录中执行。这意味着你的

source ../backup_restore_lib.sh

将会寻找一个名为/home/backup_restore_lib.sh的文件来进行源代码引用。

最好提供要引用的文件的绝对路径。

英文:

In general, crontab scripts are executed in the home directory of the user. That means that your

source ../backup_restore_lib.sh

will look for a file /home/backup_restore_lib.sh to source.

It is better to put in the absolute path to the file that you want to source.

huangapple
  • 本文由 发表于 2023年7月18日 09:26:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76709003.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定