I use latest Manjaro, and I use this script for test SSH connection quality.
#!/bin/bash
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
PATH=/bin:/usr/bin:/sbin
# Define the remote servers and output files
servers=( "1.2.3.4" "1.2.3.5" )
output_files=( "server1_output.txt" "server2_output.txt" )
# Define the user to connect as
user="admin"
# Loop over the servers
for i in "${!servers[@]}"
do
server="${servers[$i]}"
output_file="${output_files[$i]}"
# Test the SSH connection quality
ssh_start_time=$(date +%s.%N)
ssh_quality=$(ssh -q -o BatchMode=yes -o ConnectTimeout=5 "$user@$server" "echo SSH quality test successful." 2>&1)
ssh_end_time=$(date +%s.%N)
ssh_time=$(echo "$ssh_end_time - $ssh_start_time" | bc)
# Check the SSH connection quality
if echo "$ssh_quality" | grep -q "successful"; then
echo "SSH quality to $server is good. SSH time: $ssh_time seconds." >> "$output_file"
# Run the ls command and write output to file
ssh -q "$user@$server" "ls -la" >> "$output_file"
else
echo "SSH quality to $server is poor." >> "$output_file"
fi
# Close the SSH connection
ssh -q -O exit "$user@$server"
done
If run this shell script it work without problem and put result in files, this script check SSH connection quality and run
ls -la
command on the servers and put result in files.
I want crontab run this shell script every hour and put result in files. so I type this command
crontab -e
I normal user without sudo command.
I also install cronie by pacman and it is running
sudo systemctl status cronie.service  ✔
â—Ź cronie.service - Periodic Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/cronie.service; enabled; preset: disabled)
Active: active (running) since Tue 2023-05-02 15:04:00 +0330; 22h ago
Main PID: 114364 (crond)
Tasks: 1 (limit: 28660)
Memory: 1.2M
CPU: 1min 1.948s
CGroup: /system.slice/cronie.service
└─114364 /usr/bin/crond -n
Mai 03 13:34:01 faridi CROND[183386]: (mostafa) CMDOUT (/bin/sh: line 1: /sh: No such file or directory)
Mai 03 13:34:01 faridi CROND[183386]: (mostafa) CMDEND (/sh /home/mostafa/ssh-test/ssh-test3.sh)
Mai 03 13:34:01 faridi CROND[183387]: (mostafa) CMDEND (mostafa /sh /home/mostafa/ssh-test/ssh-test2.sh >> /home/mostafa/ssh-test/ssh-test.log)
Mai 03 13:34:01 faridi CROND[183386]: pam_unix(crond:session): session closed for user mostafa
Mai 03 13:34:01 faridi CROND[183387]: pam_unix(crond:session): session closed for user mostafa
Mai 03 13:35:01 faridi crond[183484]: pam_unix(crond:session): session opened for user mostafa(uid=1000) by (uid=0)
Mai 03 13:35:01 faridi CROND[183485]: (mostafa) CMD (mostafa /sh /home/mostafa/ssh-test/ssh-test2.sh >> /home/mostafa/ssh-test/ssh-test.log)
Mai 03 13:35:01 faridi CROND[183484]: (mostafa) CMDOUT (/bin/sh: line 1: mostafa: command not found)
Mai 03 13:35:01 faridi CROND[183484]: (mostafa) CMDEND (mostafa /sh /home/mostafa/ssh-test/ssh-test2.sh >> /home/mostafa/ssh-test/ssh-test.log)
Mai 03 13:35:01 faridi CROND[183484]: pam_unix(crond:session): session closed for user mostafa
but this crontab is not working and I do not see any result.
when I run this command
crontab -e
by sudo
sudo crontab -e
it works, but result are different and I see only message like this
SSH quality to 1.2.3.4 is poor
and I do not see more detail.
but when I run this script manually without crontab I see these
-rw-rw-r-- 1 admin admin 246 Nov 8 11:19 .yarnrc
SSH quality to 1.2.3.4 is good. SSH time: 10.837039616 seconds.
total 38704
drwx------. 31 admin admin 4096 May 3 09:52 .
drwxr-xr-x. 5 root root 52 Dec 11 15:24 ..
drwxr-xr-x 8 admin admin 156 May 8 2022 dffdsfds_mock
-rw-------. 1 admin admin 23734 May 2 13:28 .bash_history
-rw-r--r--. 1 admin admin 18 Mar 12 2019 .bash_logout
-rw-r--r--. 1 admin admin 193 Mar 12 2019 .bash_profile
-rw-r--r-- 1 admin admin 531 Apr 5 14:11 .bashrc
drwxrwxr-x. 6 admin admin 57 Nov 8 09:45 .cache
this is my crontab
*/1 * * * * mostafa /sh /home/mostafa/ssh-test/ssh-test2.sh
I do not know why this is happen