0

I made a shell script to connect using ssh with password to another linux server, get the details of a specific file and save it in a log file in the origin server. Manually works without problem but with the /etc/crontab doesnt update the file. (I think it may be the connection because if I try to write a test text in the file it works fine).

I tried with tee -a command and the >> command to update the file and both fails.

This is my code

#! /bin/bash

sshpass -p "password" ssh "username"@"ipserver" ls -l /filepath/file.txt | tee /home/user/test/details.log

I omitted the password, username and ip for discretion.

This is the /etc/crontab

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
 *  *  *  *  * user-name  command to be executed
*/1  *  *  *  * root    /home/test/script.sh

Hope you can help me.

Thanks

1 Answers1

0
  1. My first question is, why do you run your script as root? This opens up security holes if the script itself or the directory /home/test is not owned by root.
  2. If you are running the script as root from crontab, have you tested it as root from the command line?
  3. I see directories /home/test and /home/user/test in your post. Do they really both exist and why?

Imho, what you should do:

  1. Setup public key authentication between your user test and the remote user on the system. Use ssh-keygen to generate a public/private key pair, then copy the public key onto the target system and save it to ~/.ssh/authorized_keys. This will eliminate you having to use sshpass and have the password in cleartext in your crontab file.
  2. Test the script as user test and then install it in the user test's crontab file. Simply type crontab -e as user test and you can add the same entry there.
treuss
  • 1,913
  • 1
  • 15