Beets cron job

986 views
Skip to first unread message

Patrick Saindon

unread,
Jun 7, 2013, 7:19:33 AM6/7/13
to beets...@googlegroups.com
I need help to create a shell script that will autotag and move my files to my library.

I would set this script as a cron job and periodically tag, rename and move new downloaded music to  to my library.

Peter Schnebel

unread,
Jun 7, 2013, 7:36:34 AM6/7/13
to beets...@googlegroups.com
Hi Patrick.

Here's a (untested) suggestion:

In ~/.config/beets/config.yaml you need to set the option "move" for imports.

[snip]
import:
  move: yes
[/snip]

In the crontab you could try this (crontab -e to edit the crontab):

0 * * * * ( lockfile-create /tmp/beets && ( beet imp -q "PATH TO YOUR FILES HERE"; lockfile-remove /tmp/beets ) )

Explanation:  Every hour, try to get a lock on the lockfile /tmp/beets.  If you get the lock, call beets in quiet mode and then remove the lockfile.

Hope that helps.

Peter

parQer xQer

unread,
Jun 7, 2013, 7:44:15 AM6/7/13
to beets...@googlegroups.com
Don't know too much about cron jobs, but in macos there is a way a bit easier to do it. I set a workflow in Automator running the next applescript:

on run {input, parameters}
set a to input
set posx to POSIX path of a
tell application "Terminal"
activate
do script "beet import -t " & quoted form of posx
end tell
return input
end run

Then I only need to configure the Folder Actions on that folder to run the workflow everytime a new album folder is added. Hope it helps.

Adrian Sampson

unread,
Jun 7, 2013, 12:22:31 PM6/7/13
to beets...@googlegroups.com
One additional suggestion: you'll almost certainly want to turn the "quiet" import mode on if you're running beets automatically or in the background. Otherwise it can wait for console input that will never come.

Adrian


--
You received this message because you are subscribed to the Google Groups "beets" group.
To unsubscribe from this group and stop receiving emails from it, send an email to beets-users...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Magnus Lundberg

unread,
Jun 14, 2013, 4:12:16 PM6/14/13
to beets...@googlegroups.com
Hi,

I have used crontab to do this, but lately I have moved to use incron. I will explain both examples as precisely as I can:

With cron, I added this to /etc/crontab

*/30 * * * * your_username beet import -iql /path/to/logfile.log /path/to/downloaded/music

*/30 means it runs every thirty minutes
-i = incremental, skips folders already scanned
-q = quiet, no input required
-l = logfile

Which worked great. As I would like to have a little more details in the log file than beet can provide at the moment, I wrote this little script with a little help from friends!:

#!/bin/bash
log=/path/to/logfile.log
echo "">>$log
echo "------------ beet-auto-import.sh ---------------">>$log
beet import -iql /path/to/logfile.log /path/to/downloaded/music >>$log 2>&1
echo "Finished: $(date)">>$log
echo "-------------------------------------------------------">>$log 

Saved as beet-auto-import.sh, put in my users bin/-dir. Chmod +x beet-auto-import.sh

The "2>&1" redirects more output from the beet-command to the logfile.
All the echo-stuff is to make the log-file more readable for myself, and adding the date and time for when beet is finished is just a product of my obsessive compulsive disorder.

Not included in the example: My script also tells my logitech media server to rescan it's library to discover the newly imported music. I also tried to add a command that deleted leftover directories (under 1 mb) but it did not quite work out and deleted full directories as well. For now I clean up manually.


INOTIFY/INCRONTAB METHOD:
Inotify monitors directories for changes - I would rather have the beet-auto-import-rescan.sh script to run only when needed, instead of cluttering my logfile! For an introduction and some examples, check http://www.cyberciti.biz/faq/linux-inotify-examples-to-replicate-directories/ . It is actually quite easy, or at least it was on my headless debian wheezy 64bit box.

I will also tell you something about my setup, as I guess it will have some impact on how you have to tweak your incrontab. I use rtorrent with pyroscope, and rutorrent as a web gui for managing my torrent downloads. I have set up rtcontrol (part of pyroscope) to move completed torrents based on their LABEL as set in rutorrent (his was a bit tricky to achieve, but the details are not important for now). This happens from a regular cron job every 15 minutes. Torrents with no label wont be moved. This way I (and others in my not-so-technical family) can add torrents for download, and then label them in rutorrent if/when they feel like it. The rtcontrol cron job will move the downloaded AND labeled torrents to a comfortable hierarchy (/path/to/rtorrent/done/LABEL), and make sure the torrent continue to seed.

The torrents labeled MUSIC I want to import with beet, something which sometimes require renaming, retagging etc. Therefore I have set up incrontab to HARDLINK all files that ends up in /path/to/rtorrent/done/MUSIC to /path/to/rtorrent/done/MUSIC/process. Since it is a hardlink it wont take up extra space, and I can mess with the files as much as I like.

Here is my story:

1. Installed incron ("sudo apt-get install incron" as far as I remember)
2. Added my username in /etc/incron.allow (it contains nothing else)
3. Ran incrontab -e as my user and made it look like this:
/path/to/rtorrent/done/MUSIC IN_MOVED_TO cp -rpl $@/$# $@/process/
/path/to/rtorrent/done/MUSIC/process IN_CREATE,IN_NO_LOOP /home/user/bin/beet-auto-import.sh

$@ = path
$# = file

IN_MOVED_TO is needed because of the behavior of the aforementioned rtcontrol cronjob (which calls rtmv, which moves the downloaded files and symlinks them to their original location).
IN_NO_LOOP is needed not to spawn multiple instances of the beet-auto-import.sh script. I know it is possible to achieve this in other ways, but this was how I did it.

You will have to experiment with this based on your setup.

Now just "sudo tail -f /var/log/syslog" (on debian wheezy mind you) and "tail -f /path/to/beet/logfile.log" and see if things work.


Good luck! Truly a long read, I am not overly experienced in giving short and consistent advice.

Christophe-Marie Duquesne

unread,
Jun 18, 2013, 8:05:10 PM6/18/13
to beets...@googlegroups.com
My 2 cents: I would use 'logger -t beet', instead of using a custom
log file. This way, logrotate can take care of your stuff.

lakude

unread,
Feb 26, 2014, 3:14:12 PM2/26/14
to beets...@googlegroups.com
I had been using rtcontrol/rtmv for a long time, but I'm trying out something different for a minute.

You can skip one step by having rtorrent do the hardlinking...

Something like
schedule = watch_directory_1,10,60,"load_start=~/private/rtorrent/watch/music/*.torrent,d.set_custom1=~/private/rtorrent/complete/music/"
[more watch directories]
system.method.set_key =event.download.finished,move_complete,"d.set_directory=$d.get_custom1= ;execute=cp,-al,$d.get_base_path=,$d.get_custom1="

I guess you could do the same thing with rtcontrol, just use cp -al instead of rtmv

Then I have a rudimentary inotifywait script watching ../complete/music

WATCH=~/private/rtorrent/complete/music
while RES=$(inotifywait -e create $WATCH --format %f .)
do
        echo RES is $RES at `date`;
         ~/.local/bin/beet imp -iql ~/beet-log $WATCH/$RES

done

I need to look into incron too.  Or at least make my script auto-restartable with cron.

I wonder if there's a way to know if beets -q succeeded at importing a direcory, so we can remove the hardlink automatically.

Now I just need a way to poke subsonic to re-scan and I'll have instant musics.


Anyways, this is one of the few rtorrent discussions I could find about hardlinks, and I just wanted to post somewhere.
Reply all
Reply to author
Forward
0 new messages