0

I have a bash script that I'm trying to run remotely from another system. It is to add a cronjob to a user on the remote system.

I run this from the local system.

ssh root@remote_system 'bash -s < /home/user_name/test2.sh'

this is the script on the remote system that gets run.

#!/bin/bash
set -x
#valhost=$(hostname)
if [ -d /home/user/junk ]
then
        touch /var/spool/cron/user_name
        crontab -l -u user_name > /home/user_name/mycron
        chmod +x /home/user_name/mycron
        echo "0 0 * * * /bin/find /home/user_name \( -name '*' \) -mtime +45 -delete" >> /home/user_name/mycron
        crontab -u user_name /home/user_name/mycron
elif [ -d /home/user_name/tmp ]
then
        touch /var/spool/cron/user_name
        crontab -l -u user_name > /home/user_name/mycron
        chmod +x /home/user_name/mycron
        echo "0 0 * * * /bin/find /home/user_name \( -name '*' \) -mtime +60 -delete" >> mycron
        crontab -u user_name /home/user_name/mycron
else
    echo "directory does not exist on" $HOSTNAME  > /home/user_name/jbossjunk
fi

It checks to see if a directory is there and then puts a specific entry in the crontab. The script works fine when I run it on the actual remote system. But when I run it on the local the echo doesn't output to the "mycron" file. I've searched a lot of places and found nothing on the syntax I could use. I've tried numerous variations of syntax on the line and come up with bad results. Can someone give me the syntax that would work for this "echo" line running the script remotely

0

Try using tee. Since tee handles output differently than echo.

if [ -d /home/user/junk ]
then
    touch /var/spool/cron/user_name
    crontab -l -u user_name > /home/user_name/mycron
    chmod +x /home/user_name/mycron
    tee /home/user_name/mycron <<-EOF &>/dev/null
    0 0 * * * /bin/find /home/user_name \( -name '*' \) -mtime +45 -delete
    EOF
    crontab -u user_name /home/user_name/mycron
elif

This example is reading in the heredoc into the file specified by tee. The &>/dev/null sends the STDOUT and STDERR from the tee command to /dev/null, but doesn't affect tee writing to the specified file.

  • that worked great. Appreciate you taking the time to look at this and the resolution provided. – sfbgrousser Jun 14 '18 at 14:17
  • you're welcome. i'm glad to hear it worked for you. – Tim Kennedy Jun 14 '18 at 17:49
0

You have seven statements referencing /home/user_name/mycron (and, by the way, I assume that that is /home/user_name/mycron, and not literally user_name) and one that references just plain mycron (i.e., a relative pathname instead of an absolute one).  If you run the script from /home/user_name, they’re equivalent.  If you run the script from /root, then the echo statement writes to /root/mycron.

P.S. You absolutely do not need to do chmod +x /home/user_name/mycron.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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