Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
    
Why do you believe that the Perl scripts have not run? –  Ansgar Wiechers Jul 15 '13 at 23:28
    
I did a ps/top, and I don't see it running. Also, with the amount of data I have that needs to be deleted, I've timed the deletion script to take an average of 5 hours to complete. The script only started running an hour ago, and those are the last lines of the script. –  LeoPardus Jul 15 '13 at 23:37
5  
What the heck is sudo doing inside a script? Never put sudo 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:42
1  
@shawnhcorey Using sudo means only select parts of a script run with elevated privs instead of the whole thing. Even better would be not needing sudo at all, as you suggested by changing the file permissions. –  Schwern Jul 16 '13 at 0:36
1  
Not enough information to give an answer. What environment does the script expect? Cron's environ is usually impoverished compared to an interactive session. Is sudo expecting a tty? Cron won't give you one. Are the commands that "run fine" from a terminal yes | sudo perl ... or yes | perl ... or sudo 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
show 2 more comments

2 Answers

You simply put do

yes | ./myscript.pl

share|improve this answer
add comment
up vote 0 down vote accepted

Thanks for all the comments. I ended up changing the group and permissions of the tool and all output files. This allowed me to run the perl script without using "sudo," which others pointed out is bad practice.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.