Unity3d Asset Cache Server – Backgrounding and Firewall

As a follow up to my previous post about unity3d asset cache server, I have since figured out how to background the process, as well as opening the nessary ports in a firewall (iptables) if your distro of Linux has one enabled.

Backgrounding

To background to process you need to write a short bash script to run your installed nodejs with the CacheServer.js parameter
Below is my version of the script, i came up with after looking around online ( See the resource at the end of the post).Copy the code and paste to a file. I used “start_cache” as my file name.
This file should be in same directory and your cache folder and your CacheServer.js

#!/bin/bash
while [ ! -e /opt/cache_server/stop/stop ]; do
sleep 2
nodejs /opt/cache_server/CacheServer.js && break
done

To start the asset cache server simply run the script with background:

nohup ./script_name &

note: the above command will run cache server as the current user (the user you ran the command as).
This vesion of the script provides a way to terminate the script, both via the 2 second sleep between restarts and the check for a file named stop in a folder named stop.
Therfore to stop the script, create a folder named stop in the location you specifed, and within that folder run

touch stop

This will create a file named stop. using

ps -ef | grep node

find the pid of the process and run

kill pid_of_process

to kill the process. It will not restart when it see the file touch

note: the command i used to start the server sends all output to a file called nohup in your user home directory

cd ~
cat nohup.out

This above command will show you the output of the cache sever process
second note:
When i first tried to run this, i was getting errors in the nohup.out file about file read write errors.
Something like the error below

Error writing to file Error: EACCES, permission denied 'cache/Temp92df127b-9caf-4097-a5f2-f98c32026c05'. Possibly the disk is full?

This is caused by the user you used to run cache server not having permission to write to the “cache” folder.
In the directory where you added you cache folder, the same directory your script is in and CacheServer.js,
run the following command to give the current logged in user permission to access that folder

sudo chown -R username:username ./cache

FireWall

My particular distro (CentOS 5) has iptables configured. To allow communication between systems and the cache server, it was nessary to open both ingoing and outgoing ports on the firewall.
Unity3d Asset Cache Server uses port 8125 by default. To change this you would need to modify the AssetCache.js file. The line is near the bottom of the script (line 403 on my version)

server.listen(8125);

Changing the number in brackets would changed the port.
To open ports in iptables simply add one outgoing and one inbound rule, then restart iptables.
To add the rules edit the file iptables in /etc/sysconfig/ (default location in CentOS 5)

cd /etc/sysconfig/
nano iptables

Add the following lines, modifying them to suit your needs

-A RH-Firewall-1-INPUT -m state --state NEW,ESTABLISHED -m tcp -p tcp --dport 8125 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state ESTABLISHED -m tcp -p tcp --sport 8125 -j ACCEPT

Then save and exit and restart iptables

sudo /etc/init.d/iptables restart

This should open the required ports to allow system to communicate with asset cache server.

Hope this helps some one out.

Third note:
Incase my instructions are not clear here is my directory layout

/opt/cache_server
contains:
drwxr-xr-x 258 myusername myusername 4096 Mar  9 14:19 cache
-rw-r--r--   1 root       root       10505 Feb  2 18:09 CacheServer.js
-rwxr-xr-x   1 root       root         124 Mar  6 16:20 start_cache
drwxr-xr-x   2 root       root        4096 Mar  7 16:03 stop
drwxr-xr-x   5 root       root        4096 Mar  6 16:23 unity_cache

cache is the required directory for cache server
CacheServer is the script you run with node js
start_cache is the bash script is wrote at the begining of this post
stop if the folder I create if I need to stop cache server
unity_cache is a folder containing the original unziped download from unity3d

Resources:

http://www.cyberciti.biz/tips/linux-display-open-ports-owner.html
http://www.cyberciti.biz/faq/how-do-i-find-out-what-ports-are-listeningopen-on-my-linuxfreebsd-server/
http://www.cyberciti.biz/faq/howto-start-iptables-under-rhel-centos-linux/
http://www.cyberciti.biz/faq/howto-rhel-linux-open-port-using-iptables/
http://www.cyberciti.biz/faq/rhel-fedorta-linux-iptables-firewall-configuration-tutorial/
http://wiki.centos.org/HowTos/Network/IPTables
http://www.thegeekstuff.com/2011/03/iptables-inbound-and-outbound-rules/

http://www.unix.com/unix-dummies-questions-answers/107738-send-already-running-process-background.html
http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-7.html
http://stackoverflow.com/questions/3703227/self-restart-program-on-segfault-under-linux
http://stackoverflow.com/questions/8108051/how-can-i-run-java-command-as-a-service-centos-5
http://www.linuxquestions.org/questions/linux-enterprise-47/how-can-i-bring-the-backgroud-process-to-foreground-607782/
http://www.linuxquestions.org/questions/linux-general-1/send-process-to-background-202974/
http://linux.die.net/Bash-Beginners-Guide/sect_09_02.html

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>