I inherited a long bash script that I recently needed to modify. The bash script is run as a cronjob on a daily basis. I am decent with bash scripting, but I do not know much about Perl.
I had to substitute all "rm" commands with a call to a perl script that does something similar (for security purposes). This script was not written by me, so there is no -f flag to skip the confirmation prompt. Therefore, to automate this script I pipe "yes" to the script.
Here is an example where I am sequentially deleting two directories:
echo REMOVING FILES TO SAVE DISK SPACE
echo "yes | sudo nice -n -10 perl <path_to_delete_script.pl> -dir <del_dir1>"
yes | sudo nice -n -10 perl <path_to_delete_script.pl> -dir <del_dir1>
echo "yes | sudo nice -n -10 perl <path_to_delete_script.pl> -dir <del_dir2>"
yes | sudo nice -n -10 perl <path_to_delete_script.pl> -dir <del_dir2>
echo DONE.
In my output file, I see the following:
REMOVING FILES TO SAVE DISK SPACE
yes | sudo nice -n -10 perl <path_to_delete_script.pl> -dir <del_dir1>
yes | sudo nice -n -10 perl <path_to_delete_script.pl> -dir <del_dir2>
DONE.
It does not appear that the perl script has run. Yet when I copy and paste those two commands into the terminal, they both run fine.
Any help is appreciated. Thank you in advance.
sudo
doing inside a script? Never putsudo
in a script; it's a security breach. Your cronjob should be under root if you need those privileges and you don't. Make all the files and directories have their own group and create a user with the group name. Run the cronjob under that user: they have the privilege to remove the files. Also, never raise the priority with nice. It will bugger up your system. – shawnhcorey Jul 15 '13 at 23:42yes | sudo perl ...
oryes | perl ...
orsudo perl ...
or what, precisely? What user are you when running the commands from a shell session? What user is the cronjob invoked as? What does cron's error output say? – pilcrow Jul 16 '13 at 1:19