Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
5
rated 0 times [  5] [ 0]  / answers: 1 / hits: 29702  / 1 Year ago, sun, april 9, 2023, 12:18:24

i want to run this bash code every minute using cron. I saved it onto /root/activate.sh



#!/bin/bash

for file in /home/user/torrents/*.torrent

do
if [ "$file" != "/home/user/torrents/*.torrent" ]; then
echo [`date`] "$file" added to queue. >> /var/log/torrentwatch.log
/usr/bin/transmission-remote localhost:9091 -a "$file"
mv "$file" "$file".added
sleep 1
fi
done


permission is set -rwxrwxrwx 1 root root 278 May 27 01:27 activate.sh



then inside my crontab -e i placed this



* * * * * root sh /root/activate.sh



the script does not execute and i get this log error



May 27 01:40:02 media CRON[3556]: (root) CMD (root sh /root/activate.sh)
May 27 01:40:02 media CRON[3555]: (CRON) info (No MTA installed, discarding output)
---after a minute---
May 27 01:41:01 media CRON[3582]: (root) CMD (root sh /root/activate.sh)
May 27 01:41:01 media CRON[3581]: (CRON) info (No MTA installed, discarding output)

More From » bash

 Answers
5

First off, why aren't you just using transmission's watch-dir feature?



The crontab entry is wrong, it should be * * * * * /root/activate.sh when you add it with crontab -e. /etc/crontab and /etc/cron.d/* takes an extra username field, but the user specific crontabs which you set with the crontab command, does not have a username field; the jobs are run as the user that ran crontab.



Also, since this script operates on files in user's homedir, I would've run the job as that user. There's nothing about that script that requires root permissions, apart from maybe writing to that log file, but you can just change ownership of that logfile instead.



As for the script, I'd modify it a bit:



#!/bin/bash

for file in ~user/torrents/*.torrent; do
[[ -f "$file" ]] || continue
transmission-remote -a "$file" && mv "$file" "$file.added" || continue
printf '[%s] %s added to queue
' "$(date)" "$file"
sleep 1
done >> /var/log/torrentwatch.log


Lastly, you should avoid adding extensions for scripts, and especially not use .sh when the script is a bash script, not an sh script.


[#31033] Sunday, April 9, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ampust

Total Points: 133
Total Questions: 109
Total Answers: 111

Location: Reunion
Member since Fri, Jul 22, 2022
2 Years ago
ampust questions
Thu, Jul 15, 21, 16:30, 3 Years ago
Mon, Oct 24, 22, 05:38, 2 Years ago
Thu, Sep 15, 22, 14:37, 2 Years ago
Tue, Sep 7, 21, 17:46, 3 Years ago
;