running remote bash script from local linux host and issue with "echo command not working
Clash 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
shell-script
add a comment |Â
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
shell-script
add a comment |Â
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
shell-script
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
shell-script
edited Jun 14 at 14:03
asked Jun 13 at 19:44
sfbgrousser
12
12
add a comment |Â
add a comment |Â
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.
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
add a comment |Â
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
.
add a comment |Â
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.
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
add a comment |Â
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.
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
add a comment |Â
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.
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.
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
add a comment |Â
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
add a comment |Â
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
.
add a comment |Â
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
.
add a comment |Â
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
.
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
.
answered Jun 13 at 21:36
Scott
6,21332347
6,21332347
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password