running remote bash script from local linux host and issue with "echo command not working

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












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







share|improve this question

























    up vote
    0
    down vote

    favorite












    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







    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      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







      share|improve this question













      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









      share|improve this question












      share|improve this question




      share|improve this question








      edited Jun 14 at 14:03
























      asked Jun 13 at 19:44









      sfbgrousser

      12




      12




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote













          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.






          share|improve this answer





















          • that worked great. Appreciate you taking the time to look at this and the resolution provided.
            – sfbgrousser
            Jun 14 at 14:17










          • you're welcome. i'm glad to hear it worked for you.
            – Tim Kennedy
            Jun 14 at 17:49

















          up vote
          0
          down vote













          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 u s e r _ n a m e)
          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.






          share|improve this answer





















            Your Answer







            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "106"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );








             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f449652%2frunning-remote-bash-script-from-local-linux-host-and-issue-with-echo-command-no%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            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.






            share|improve this answer





















            • that worked great. Appreciate you taking the time to look at this and the resolution provided.
              – sfbgrousser
              Jun 14 at 14:17










            • you're welcome. i'm glad to hear it worked for you.
              – Tim Kennedy
              Jun 14 at 17:49














            up vote
            0
            down vote













            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.






            share|improve this answer





















            • that worked great. Appreciate you taking the time to look at this and the resolution provided.
              – sfbgrousser
              Jun 14 at 14:17










            • you're welcome. i'm glad to hear it worked for you.
              – Tim Kennedy
              Jun 14 at 17:49












            up vote
            0
            down vote










            up vote
            0
            down vote









            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.






            share|improve this answer













            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.







            share|improve this answer













            share|improve this answer



            share|improve this answer











            answered Jun 13 at 20:26









            Tim Kennedy

            13.3k22949




            13.3k22949











            • that worked great. Appreciate you taking the time to look at this and the resolution provided.
              – sfbgrousser
              Jun 14 at 14:17










            • you're welcome. i'm glad to hear it worked for you.
              – Tim Kennedy
              Jun 14 at 17:49
















            • that worked great. Appreciate you taking the time to look at this and the resolution provided.
              – sfbgrousser
              Jun 14 at 14:17










            • you're welcome. i'm glad to hear it worked for you.
              – Tim Kennedy
              Jun 14 at 17:49















            that worked great. Appreciate you taking the time to look at this and the resolution provided.
            – sfbgrousser
            Jun 14 at 14:17




            that worked great. Appreciate you taking the time to look at this and the resolution provided.
            – sfbgrousser
            Jun 14 at 14:17












            you're welcome. i'm glad to hear it worked for you.
            – Tim Kennedy
            Jun 14 at 17:49




            you're welcome. i'm glad to hear it worked for you.
            – Tim Kennedy
            Jun 14 at 17:49












            up vote
            0
            down vote













            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 u s e r _ n a m e)
            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.






            share|improve this answer

























              up vote
              0
              down vote













              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 u s e r _ n a m e)
              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.






              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                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 u s e r _ n a m e)
                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.






                share|improve this answer













                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 u s e r _ n a m e)
                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.







                share|improve this answer













                share|improve this answer



                share|improve this answer











                answered Jun 13 at 21:36









                Scott

                6,21332347




                6,21332347






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f449652%2frunning-remote-bash-script-from-local-linux-host-and-issue-with-echo-command-no%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    How to check contact read email or not when send email to Individual?

                    Bahrain

                    Postfix configuration issue with fips on centos 7; mailgun relay