Visit The New, Modern Unix Linux Community


ln -s creates symlink in symlink, if [ -f ... ] says file that exists doesn't exist


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ln -s creates symlink in symlink, if [ -f ... ] says file that exists doesn't exist
# 1  
ln -s creates symlink in symlink, if [ -f ... ] says file that exists doesn't exist

Hi Forums,

I got a little problem, I made a few modifications to the code of the launch script of a testing server(minecraft) and now updating is broken aswell as the automatic directory creation.

These Lines somehow create an endless symlink that refers to itself and I don't know how to fix that:
Code:
        for INDEX in ${!WORLDNAME[@]}
        do
                if [ ! -d "$BACKUPPATH/${WORLDNAME[$INDEX]}" ]; then
                        as_user "mkdir $BACKUPPATH/${WORLDNAME[$INDEX]}"
                fi
                if [ ! -d "$WORLDSTORAGE/${WORLDNAME[$INDEX]}" ]; then
                        as_user "mkdir $WORLDSTORAGE/${WORLDNAME[$INDEX]}"
                fi
                if [ ! -d "$RAMDISK/${WORLDNAME[$INDEX]}" ]; then
                        as_user "mkdir RAMDISK/${WORLDNAME[$INDEX]}"
                fi
                if [ ! -L "$MCPATH/${WORLDNAME[$INDEX]}" ]; then
                        as_user "ln -s $RAMDISK/${WORLDNAME[$INDEX]} $MCPATH/${WORLDNAME[$INDEX]}"
                fi
        done

Also if [ -f ... ] reports that a file that was just downloaded doesn't exist:
Code:
                as_user "wget -O $MCPATH/craftbukkit.jar.update $DOWNLOADURL"
                if [ -f "$MCPATH/craftbukkit.jar.update" ]; then

This has bothered me for the past 3 weeks but I couldn't come up with a solution or even an answer to why it is doing it, so I guess I need help :/

The full Script:
Code:
#!/bin/bash
# /etc/init.d/minecraft

### BEGIN INIT INFO
# Provides:   minecraft
# Required-Start: $local_fs $remote_fs
# Required-Stop:  $local_fs $remote_fs
# Should-Start:   $network
# Should-Stop:    $network
# Default-Start:  2 3 4 5
# Default-Stop:   0 1 6
# Short-Description:    Minecraft server
# Description:    Init script for minecraft/bukkit server, with rolling logs and use of ramdisk for less lag.
### END INIT INFO

#############################
######### SETTINGS ##########
#############################
# SERVICE       - Name of the server software jar file
# SCREEN        - Name to use for the screen instance
# USERNAME      - User that we should run the server under
# MCPATH        - Path to the server software's root
# CPU_COUNT     - Count of CPUs/Cores to use for Java
# INITMEM       - Initial memory to take up in RAM (does not include ramdisk)
# MAXMEM        - Maximal memory to take up in RAM (does not include ramdisk)
# INVOCETION    - The command used to start the server software
# BACKUPPATH    - Where should we store per-world backups?
# LOGPATH       - Where should log-roll's be stored to?
# WHOLEBACKUP   - Where should we complete server backups to?
# WORLDSTORAGE  - Where should the physical world copies be at?
# WORLDNAME[@]  - How are all the Worlds named? (Replace @ with an increasing number)
# WORLDRAM[@]   - Should the world @ be stored on the ramdisk? (Replace @ with World number, see WORLDNAME)
# RAMDISK       - Where is the ramdisk located?
# KEEPBACKUPTIME- How old is the last backup allowed to be? (anything past this amount of days gets deleted)
# RETRYSTOP     - How often should we check for the server to be shut down after we told it to 'stop'?
# RETRYWAIT     - How much time lies between each check?
#################
MCPATH="~/server"
BACKUPPATH="~/worldBackup"
LOGPATH="~/logBackup"
WHOLEBACKUP="~/serverBackup"
WORLDSTORAGE="~/worldStorage"
RAMDISK="~/ramdisk/host1"
SCREEN="host1-server"
SERVICE="craftbukkit.jar"
USERNAME="host1"
CPU_COUNT=3
INITMEM="128M"
MAXMEM="4096M"
INVOCATION="java -Xmx$MAXMEM -Xms$INITMEM -XX:+UseConcMarkSweepGC -XX:+CMSIncrementalPacing -XX:ParallelGCThreads=$CPU_COUNT -XX:+AggressiveOpts -jar $SERVICE nogui"
WORLDNAME[1]="Normal"
WORLDNAME[2]="Normal_nether"
WORLDNAME[3]="Skylands"
WORLDRAM[1]=true
WORLDRAM[2]=true
WORLDRAM[3]=true
KEEPBACKUPTIME=30
RETRYSTOP=3
RETRYWAIT=5
DOWNLOADURL="http://ci.bukkit.org/job/dev-CraftBukkit/lastStableBuild/artifact/target/craftbukkit-1.0.0-SNAPSHOT.jar"
################

ME=`whoami`
as_user() {
        if [ $ME == $USERNAME ] ; then
                bash -c "$1"
        else
                su - $USERNAME -c "$1"
        fi
}

#Setup folders and links
do_setup_folders() {
        if [ ! -d "$BACKUPPATH" ]; then
                as_user "mkdir $BACKUPPATH"
        fi
        if [ ! -d "$LOGPATH" ]; then
                as_user "mkdir $LOGPATH"
        fi
        if [ ! -d "$WHOLEBACKUP" ]; then
                as_user "mkdir $WHOLEBACKUP"
        fi
        if [ ! -d "$WORLDSTORAGE" ]; then
                as_user "mkdir $WORLDSTORAGE"
        fi
        if [ ! -d "$RAMDISK" ]; then
                as_user "mkdir $RAMDISK"
        fi
        for INDEX in ${!WORLDNAME[@]}
        do
                if [ ! -d "$BACKUPPATH/${WORLDNAME[$INDEX]}" ]; then
                        as_user "mkdir $BACKUPPATH/${WORLDNAME[$INDEX]}"
                fi
                if [ ! -d "$WORLDSTORAGE/${WORLDNAME[$INDEX]}" ]; then
                        as_user "mkdir $WORLDSTORAGE/${WORLDNAME[$INDEX]}"
                fi
                if [ ! -d "$RAMDISK/${WORLDNAME[$INDEX]}" ]; then
                        as_user "mkdir RAMDISK/${WORLDNAME[$INDEX]}"
                fi
                if [ ! -L "$MCPATH/${WORLDNAME[$INDEX]}" ]; then
                        as_user "ln -s $RAMDISK/${WORLDNAME[$INDEX]} $MCPATH/${WORLDNAME[$INDEX]}"
                fi
        done
}

server_running() {
        if ps ax | grep -v grep | grep $SCREEN | grep $USERNAME | grep $SERVICE > /dev/null
        then
                return 0
        else
                return 1
        fi
}

datepath() {
        echo $1`date +%FT%T`$3
}

mc_start() {
        if server_running
        then
                echo "Tried to start but $SERVICE was already running!"
        else
                echo "$SERVICE was not running... starting."
                as_user "cd $MCPATH && screen -dmS $SCREEN $INVOCATION"
                sleep 7
                if server_running
                then
                        echo "$SERVICE is now running."
                else
                        echo "Could not start $SERVICE."
                fi
        fi
}

mc_saveoff() {
        if server_running
        then
                echo "$SERVICE is running... suspending saves"
                as_user "screen -p 0 -S $SCREEN -X eval 'stuff \"save-off\"\015'"
                as_user "screen -p 0 -S $SCREEN -X eval 'stuff \"save-all\"\015'"
                sleep 10
                sync
        else
                echo "$SERVICE was not running. Not suspending saves."
        fi
}

mc_saveon() {
        if server_running
        then
                echo "$SERVICE is running... re-enabling saves"
                as_user "screen -p 0 -S $SCREEN -X eval 'stuff \"save-on\"\015'"
        else
                echo "$SERVICE was not running. Not resuming saves."
        fi
}

mc_stop() {
        if server_running
        then
                echo "$SERVICE is running... stopping."
                as_user "screen -p 0 -S $SCREEN -X eval 'stuff \"save-all\"\015'"
                sleep 10
                as_user "screen -p 0 -S $SCREEN -X eval 'stuff \"stop\"\015'"
                sleep $RETRYWAIT
        else
                echo "$SERVICE was not running."
        fi
        for (( i = 0; i <= $RETRYSTOP; i++ ))
        do
                if server_running
                then
                        echo "Waiting for $SERVICE to shut down, try $i of $RETRYSTOP"
                else
                        echo "$SERVICE stopped."
                        return
                fi
                sleep $RETRYWAIT
        done
}
log_roll() {
        as_user "mkdir -p $LOGPATH"
        path=`datepath $LOGPATH/server_ .log.gz .log`
        as_user "mv $MCPATH/server.log $path && gzip $path"
}
mc_whole_backup() {
        as_user "mkdir -p $WHOLEBACKUP"
        path=`datepath $WHOLEBACKUP/ .tar.bz2 .tar.bz2`
        as_user "tar -hcjf $path $MCPATH"
}
mc_world_backup() {
        as_user "mkdir -p $BACKUPPATH"
        for INDEX in ${!WORLDNAME[@]}
        do
                echo "Backing up minecraft ${WORLDNAME[$INDEX]}"
                oldfile=`ls $BACKUPPATH/${WORLDNAME[$INDEX]} -tr | tail -n 1`
                path=`datepath $BACKUPPATH/${WORLDNAME[$INDEX]}/ .tar.bz2 .tar.bz2`
                as_user "tar -hcjf $path $MCPATH/${WORLDNAME[$INDEX]}"
                if ! `diff '$BACKUPPATH/${WORLDNAME[$INDEX]}/$oldfile' '$path' >/dev/null`
                then
                        as_user "rm -f '$path'"
                fi
                as_user "find '$BACKUPPATH/${WORLDNAME[$INDEX]}' -mtime +$KEEPBACKUPTIME -exec rm -f {} \;"
        done
}
check_links() {
        do_setup_folders
}
to_ram() {
        for INDEX in ${!WORLDNAME[@]}
        do
                if ${WORLDRAM[$INDEX]}
                then
                        if [ -L $MCPATH/${WORLDNAME[$INDEX]} ]
                        then
                                as_user "rsync -rt ${WORLDSTORAGE}/${WORLDNAME[$INDEX]}/ $RAMDISK/${WORLDNAME[$INDEX]}"
                                echo "${WORLDNAME[$INDEX]} copied to ram"
                        fi
                fi
        done
}
to_disk() {
        for INDEX in ${!WORLDNAME[@]}
        do
                as_user "rsync -rt $MCPATH/${WORLDNAME[$INDEX]}/ ${WORLDSTORAGE}/${WORLDNAME[$INDEX]}"
                echo "${WORLDNAME[$INDEX]} copied to disk"
        done
}
mc_update() {
        if server_running
        then
                echo "$SERVICE is running! Will not start update."
        else
                echo "Updating craftbukkit...."
                as_user "wget -O $MCPATH/craftbukkit.jar.update $DOWNLOADURL"
                if [ -f "$MCPATH/craftbukkit.jar.update" ]; then
                        if `diff $MCPATH/$SERVICE $MCPATH/craftbukkit.jar.update > /dev/null`
                        then
                                echo "You are already running the latest version of CraftBukkit."
                                as_user "rm $MCPATH/craftbukkit.jar.update"
                        else
                                as_user "mv $MCPATH/craftbukkit.jar.update $MCPATH/$SERVICE"
                                echo "CraftBukkit successfully updated."
                        fi
                else
                        echo "CraftBukkit update could not be downloaded."
                fi
        fi
}

case "$1" in
        start)
                # Starts the server
                check_links
                to_ram
                mc_start
                ;;
        stop)
                # Stops the server
                as_user "screen -p 0 -S $SCREEN -X eval 'stuff \"say SERVER SHUTTING DOWN!\"\015'"
                mc_stop
                to_disk
                ;;
        restart)
                # Restarts the server
                as_user "screen -p 0 -S $SCREEN -X eval 'stuff \"say SERVER REBOOT IN 10 SECONDS.\"\015'"
                mc_stop
                mc_start
                ;;
        backup)
                # Backups world
                as_user "screen -p 0 -S $SCREEN -X eval 'stuff \"say Backing up world.\"\015'"
                mc_saveoff
                to_disk
                mc_world_backup
                mc_saveon
                as_user "screen -p 0 -S $SCREEN -X eval 'stuff \"say Backup complete.\"\015'"
                ;;
        whole-backup)
                # Backup everything
                mc_whole_backup
                ;;
        update)
                #update minecraft_server.jar and craftbukkit.jar (thanks karrth)
                as_user "screen -p 0 -S minecraft -X eval 'stuff \"say SERVER UPDATE IN 10 SECONDS.\"\015'"
                mc_stop
                to_disk
                mc_update
                mc_start
                ;;
        to-disk)
                # Writes from the ramdisk to disk, in case the server crashes.
                # Using ramdisk speeds things up a lot, especially if you allow
                # teleportation on the server.
                mc_saveoff
                to_disk
                mc_saveon
                ;;
        connected)
                # Lists connected users
                as_user "screen -p 0 -S $SCREEN -X eval 'stuff list\015'"
                sleep 3s
                tac $MCPATH/server.log | grep -m 1 "Connected"
                ;;
        log-roll)
                # Moves and Gzips the logfile, a big log file slows down the
                # server A LOT (what was notch thinking?)
                as_user "screen -p 0 -S $SCREEN -X eval 'stuff \"say ROUTINE REBOOT IN 10 SECONDS.\"\015'"
                mc_stop
                log_roll
                mc_start
                ;;
        status)
                # Shows server status
                if server_running
                then
                        echo "$SERVICE is running."
                else
                        echo "$SERVICE is not running."
                fi
                ;;
        version)
                echo Craftbukkit version `awk '/Craftbukkit/ {sub(/\)/, ""); print $12}' $MCPATH/server.log`
                ;;
        links)
                check_links
                ;;
        help)
                echo "Usage: /etc/init.d/minecraft command"
                echo
                echo "start - Starts the server"
                echo "stop - stops the server"
                echo "restart - restarts the server"
                echo "backup - backups the worlds defined in the script"
                echo "whole-backup - backups the entire server folder"
                echo "update - fetches the latest version of minecraft.jar server and Bukkit"
                echo "log-roll - Moves and gzips the logfile"
                echo "to-disk - copies the worlds from the ramdisk to worldstorage"
                echo "connected - lists connected users"
                echo "status - Shows server status"
                echo "version - returs Bukkit version"
                echo "links - creates nessesary symlinks"
                ;;
        *)
                echo "No such command see /etc/init.d/minecraft help"
                exit 1
                ;;
esac

exit 0

I appreciate any help you could give me, I'm relatively new to Shell Scripting and this error goes past my previous experience of logical. Thanks in Advance,

Xaymar

Last edited by Xaymar; 11-22-2011 at 04:00 PM.. Reason: Parser is messing up my text. Let's see if it will mess this up aswell...

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #581
Difficulty: Easy
In a typical unix-based system, everything is a file.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to create symlink for latest file only?

Hello, On Solaris 10, here are entries for logs in httpd.conf ErrorLog "|/export/apache/apache-2.2.17/bin/rotatelogs -l -f /var/log/apache/error_log.%Y%m%d 86400" It keeps creating daily logs with below names - -rw-r--r-- 1 root root 1016747232 Apr 5 23:59... (16 Replies)
Discussion started by: solaris_1977
16 Replies

2. UNIX for Beginners Questions & Answers

Symlink

I know how to create a symlink but I am getting an error message that the file already exists. Also, my symlink doesn't point from target directory to the path correctly, Here is an example of the path to my folder structure path: cd /wkspce/wff/DEV/jobs/DEL the folder structure is: ... (3 Replies)
Discussion started by: dellanicholson
3 Replies

3. Shell Programming and Scripting

Symlink creation

I am trying to setup my plex server to use symlinks rather than host the movie files. in my storage directory, i have both movies(some in subdirectory of the name and some just in the parent directory) and tvshows, which have subdirectories for each season, which contains the episodes i would... (3 Replies)
Discussion started by: bandion
3 Replies

4. UNIX for Dummies Questions & Answers

Remove symlink and target

i would like to remove a directory and also symlink target inside. my_directory -- file1 -> /targetpath/file1 -- file2 -> /targetpath/file2 rm -rf my_directory will not remove symlink target. rm -rf "`readlink -f file1`" will only remove target if specifying the symlink is specified ... (4 Replies)
Discussion started by: lsy
4 Replies

5. Ubuntu

Creating conditional symlink

Hi All, Is there any way to create a symlink that will point to last 1000 line of a log file. My symlink will always point to "tail -1000 logfile". This can be achieved by writing a script and scheduling with high frequency, but I am looking for some other alternatives. Please let me know... (8 Replies)
Discussion started by: sussus2326
8 Replies

6. UNIX and Linux Applications

Tomcat 6.0 fails to read symlink(symbolic link) file

Hello all experts, Im in a situation where Tomcat simply does not want to read this file through the symlink.... I checked permissions..OK Also checked file & tomcat owner...all OK. This is what I have my /tomcat/conf/Catalina/local/appname.xml <Context> <Resource name="jdbc/black" ... (3 Replies)
Discussion started by: KingaKoopa
3 Replies

7. Shell Programming and Scripting

File count for symlink using find command

Hi Guys, The script which I am using works really good for finding the file count for files that are not symlink. I know I can use find command like: find . -type l | wc -l This way I can get filecount of the symlink but is there a one liner to use -type l and -type f option ? That is... (4 Replies)
Discussion started by: dixits
4 Replies

8. Solaris

symlink on link file

Hi all, I want to create a symlink on a link file, i mean, there is a file which is actually a symlink of some version. Now i want to create one more symlink on that link file. EX: there is a file: uat -> version prod -> version Now i want to create one more link on these 'uat' and... (1 Reply)
Discussion started by: raghu.iv85
1 Replies

9. Shell Programming and Scripting

sed -i destroy symlink - how to solve this ?

After a few hours of frustration because I didn't understand why my symlinks are destroyed i just found out that sed -i will destroy symlinks. I searched but i didn't found any good solution for this. Is there any way to overcome this ? On my ubuntu server sed version is 4.1.5 edit: Sorry... (0 Replies)
Discussion started by: ktm
0 Replies

10. UNIX for Dummies Questions & Answers

creating symlink

hi... I have a folder<abc> under /root folder. I want to create a symlink such that when i click on folder<abc> under root, should display my home folder (home/krish). Immediate inputs appreciated..... (1 Reply)
Discussion started by: rama.honge
1 Replies

Featured Tech Videos