INTERACT FORUM

Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1]   Go Down

Author Topic: Request for tutorial on setting up cron job for MediaCenter on QNAP  (Read 9629 times)

Afrosheen

  • Regular Member
  • Galactic Citizen
  • ****
  • Posts: 313

I'm thankful for Bob to have found the key command to request this, but as a command line newbie, I'm wondering if those of you who know better can give a hand to provide a tutorial to set up a cron job to keep MC running on my QNAP.  Bob mentioned a few things that are required to be done first in order for this to run successfully.  I've listed them out below, but I'm wondering how one would go about doing this.  I'm sure this would be beneficial for others who have MC installed on their QNAP NAS. 


Prerequisites:
1. How to obtain process ID of mediacenter22 via pidof command?
2. How to find out if the X server is running on the QNAP, i.e. the user is logged in?

Setting up the script:
1. Command to see if the process ID of mediacenter22 exists;
2. If not running, then run the following command:
Code: [Select]
chroot /share/CACHEDEV1_DATA/.qpkg/HD_Station su - -c "export DISPLAY=:0 ; nohup mediacenter22&"3. Configure script via cron or launchd command to run every 5 minutes, or user defined.

I only know about cron and launchd through apps like LingonX and Cronnix on the MacOS which are just GUI frontends for the command.  I admit I should learn more about using the command line personally, but I'm having a hard time investing the necessary time right now. 

Thanks again for the consideration!
-A
Logged

imugli

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 1598

You'll want to try something like this (providing you have access to a terminal)...

Create a bash script (we'll call it MC.sh) with the following...

#!/bin/bash
pidof  mediacenter22 >/dev/null
if [[ $? -ne 0 ]] ; then
        echo "Restarting MC:     $(date)" >> /var/log/MC.txt
chroot /share/CACHEDEV1_DATA/.qpkg/HD_Station su - -c "export DISPLAY=:0 ; nohup mediacenter22&"
fi

This checks for a pid, and if 0 (or doesn't exist) starts MC. Make it executable by running

chmod u+x your/location/to/MC.sh

Then, run
crontab -e

and add the following line to it

*/5 * * * * /your/path/to/MC.sh


I haven't tested this, but adapted it from http://stackoverflow.com/questions/20162678/linux-script-to-check-if-process-is-running-act-on-the-result.

aoqw76

  • Galactic Citizen
  • ****
  • Posts: 257

I have a cron job that has the same goal, but works by looking for the existence of crash logs and relaunching mc if one is found, or indeed launching if its not running at all.
Code: [Select]
zzip="/home/robert/Documents/mc22crash.zip"
touch /var/crash/dummy_file
## this counts how many crash logs exist for mediacenter
checkmc=`ls -ltr /var/crash/* | grep mediacenter22 | wc -l`
## this counts if mediacenter is one of the running applications
checkmcv2=`ps -aux | grep -v grep | grep mediacenter | wc -l`
## you dont need to do it, but i move the crash file and zip it, so you can skip/remove that code if not needed. it does help tidy up the crash logs though.
now=$(date +%y%m%d%H%M)
## if it has crashed i.e. crash log found
if [ $checkmc -gt 0 ]
then
## apport is the app that pops up in ubuntu to notify you that something has gone wrong, but i just get rid
pkill apport
pkill mediacenter
## this block is moving and zipping the log file
cd /var/crash
for file in *mediacenter22*
do
  chown robert:robert "$file"
  mv "$file" "$file.$now"
done
mv *mediacenter22* /home/robert/Documents
rm $zzip
zip /home/robert/Documents/mc22crash /home/robert/Documents/$file.$now
fi
## now if either a crash log was found, or mediacenter was not found as a running application, restart it
if [ $checkmc -eq 0 ] | [ $checkmcv2 -eq 0 ]
then
/usr/bin/mediacenter22
fi
Logged
xubuntu lts 14.04 32 bit, running mc22.0.36 as anything later doesn't work properly over vnc. using linux mc22 as media server to windows mc22 last version / jremote on ipad.
I am the owner / sole admin for www.cyrusunofficial.co.uk ("fan" site for Cyrus Audio hifi)

astromo

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 2251

Based on this:
https://www.techandme.se/qnap-and-cron/
Looks like cron on a qnap is far from straight forward.

One to investigate. Thanks for highlighting the possibilities.
Logged
MC33, Win10 x64, HD-Plex H5 Gen2 Case, HD-Plex 400W Hi-Fi DC-ATX / AC-DC PSU, Gigabyte Z370 ULTRA Gaming 2.0 MoBo, Intel Core i7 8700 CPU, 4x8GB GSkill DDR4 RAM, Schiit Modi Multibit DAC, Freya Pre, Nelson Pass Aleph J DIY Clone, Ascension Timberwolf 8893BSRTL Speakers, BJC 5T00UP cables, DVB-T Tuner HDHR5-4DT

Afrosheen

  • Regular Member
  • Galactic Citizen
  • ****
  • Posts: 313
Re: Request for tutorial on setting up cron job for MediaCenter on QNAP
« Reply #4 on: July 09, 2017, 11:09:34 pm »

Based on this:
https://www.techandme.se/qnap-and-cron/
Looks like cron on a qnap is far from straight forward.

One to investigate. Thanks for highlighting the possibilities.

Yes, I was having much trouble figuring out how to use QNAP's crontab so I went with a different route with tonight being the first test run that went successfully.  I hope that with this it will be the end of having to manually start up MediaCenter on my QNAP.

First, I went with a different crontab for QNAP: WebCrontab, which I installed via QNAP's AppCenter.  The qpkg is only located on QNAP EU, not on the default repository.  To add QNAP EU repository, you'll need to open AppCenter's settings that's located on the top right by clicking on the gear icon.  Then add the following link with your preferred title: http://store.qnapclub.eu/store.php

Once that's done, search for WebCrontab from the newly added repository and install it.  Add port 4000 to your router's forwarding list. Then add the script that @imugli wrote as a task.  Here's a screenshot of my setup on WebCrontab.

So far each time I manually quit MC, it pops right back up.  I feel like I conquered an indomitable foe.

Logged

Afrosheen

  • Regular Member
  • Galactic Citizen
  • ****
  • Posts: 313
Re: Request for tutorial on setting up cron job for MediaCenter on QNAP
« Reply #5 on: December 26, 2017, 12:50:23 am »

I just updated the firmware of my QNAP NAS and I can't get this script to work anymore.  I get an error when I try to run the command that I used to restart MC when it wasn't running:

Code: [Select]
[~] # chroot /share/CACHEDEV1_DATA/.qpkg/HD_Station su - -c "export DISPLAY=:0 ; nohup mediacenter22&"
-su: cannot redirect standard input from /dev/null: No such file or directory
[~] # nohup: failed to render standard input unusable: No such file or directory

Anyone know why this is happening and a possible solution?

Thanks!
Logged

Afrosheen

  • Regular Member
  • Galactic Citizen
  • ****
  • Posts: 313
Re: Request for tutorial on setting up cron job for MediaCenter on QNAP
« Reply #6 on: December 29, 2017, 04:19:31 pm »

For those still holding on to MediaCenter as a qpkg with a similar setup like mine, do not upgrade Hybrid Diskstation past 3.1c as QNAP moved HDStation to a closed source under the control of "hdsfusemnt." 

Further details are located here.
Logged
Pages: [1]   Go Up