I run a webserver by one Makefile by command make install
The content of Makefile is
project : webserver.c
webserver.c : server/webserver.c
gcc -g -D_FORTIFY_SOURCE=0 -frecord-gcc-switches -fno-stack-protector -g -o server/webserver -lpthread -lnsl -lresolv -D_TS_ERRNO server/webserver.c
install :
sudo /etc/init.d/fhttpd.init stop
sudo cp server/webserver /usr/local/fhttpd/fhttpd
sudo chmod 4755 /usr/local/fhttpd/fhttpd
sudo /etc/init.d/fhttpd.init start
clean :
rm server/webserver
the content of fhttpd.init is
httpd="/usr/local/fhttpd/fhttpd"
prog=fhttpd
port=8080
RETVAL=0
# disable ASLR so that lab exercise will work reliably
echo 0 > /proc/sys/kernel/randomize_va_space
start() {
echo "Stopping $prog..."
killall $prog
echo -n $"Starting $prog: "
$httpd $port &
}
stop() {
echo -n $"Stopping $prog: "
killall $prog
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
start
;;
reload)
start
;;
*)
echo $"Usage: $prog {start|stop|restart}"
exit 1
esac
exit $RETVAL
I guess init use & to make server run in background. But after I use that command. I used jobs command to show all my jobs, I can't see anything. But if I run it directly, like this
lhu343ai#server:~$ /usr/local/fhttpd/fhttpd 8080 &
[1] 7154
lhu343ai#server:~$ jobs
[1]+ Running /usr/local/fhttpd/fhttpd 8080 &
Everything is OK to me.
If I just use make install or sudo /etc/init.d/fhttpd.init start, jobs shows nothing. In fact, the server is running correctly. I just don't know how does it work because I use & in /etc/init.d/fhttpd.init.
I don't know why is the reason I can't use 'jobs' with makefile. And how can I get background program back in this case.
I'd expect this command to elevate execution to the superuser's userspace:
sudo /etc/init.d/fhttpd.init start
As a result you wouldn't see that process in your regular user, lhu343ai. You'd want to check the jobs for the superuser, instead. For example, sudo jobs would be the command you're looking for...
Please note that this isn't a programming question, and it certainly isn't about C. We have enough low-level noise spammed over that tag from people confusing x86 assembly concepts with C, as it is.
Related
I have WSL bash running in a cmd. I don't use it for anything, it just hangs there to keep the WSL system alive.
When I start X applications:
bash -c "DISPLAY=:0 xmessage hello &"
I get this result:
I can close down the command window without any problems, but it's rather annoying.
How can run commands without getting this cmd window every time?
Here's a simpler solution, which, however, requires a WSH-based helper script, runHidden.vbs (see bottom section):
wscript .\runHidden.vbs bash -c "DISPLAY=:0 xmessage 'hello, world'"
To apply #davv's own launch-in-background technique to avoid creating a new bash instance every time:
One-time action (e.g., at boot time): launch a hidden, stay-open bash window. This spawns 2 bash processes: the Windows bash.exe process that owns the console window, and the WSL bash process (owned by the WSL init singleton), which is then available for servicing background commands.
wscript .\runHidden.vbs bash # hidden helper instance for servicing background commands
For every X Window-launching command: Terminate each command with & to have it be run by the hidden WSL bash instance asynchronously, without keeping the invoking bash instance alive:
wscript .\runHidden.vbs bash -c "DISPLAY=:0 xmessage 'hello, world' &"
runHidden.vbs source code:
' Simple command-line help.
select case WScript.Arguments(0)
case "-?", "/?", "-h", "--help"
WScript.echo "Usage: runHidden executable [...]" & vbNewLine & vbNewLine & "Runs the specified command hidden (without a visible window)."
WScript.Quit(0)
end select
' Separate the arguments into the executable name
' and a single string containing all arguments.
exe = WScript.Arguments(0)
sep = ""
for i = 1 to WScript.Arguments.Count -1
' Enclose arguments in "..." to preserve their original partitioning, if necessary.
if Instr(WScript.Arguments(i), " ") > 0 then
args = args & sep & """" & WScript.Arguments(i) & """"
else
args = args & sep & WScript.Arguments(i)
end if
sep = " "
next
' Execute the command with its window *hidden* (0)
WScript.CreateObject("Shell.Application").ShellExecute exe, args, "", "open", 0
Even when launched from a GUI app (such as via the Run dialog invoked with Win+R), this will not show a console window.
If your system is configured to execute .vbs scripts with wscript.exe by default (wscript //h:wscript /s, which, I think, is the default configuration), you can invoke runHidden.vbs directly, and if you put it in your %PATH%, by filename (root) only: runHidden ....
Note that use of the script is not limited to console applications: even GUI applications can be run hidden with it.
There's another simple solution, it requires an external executable though. It has no dependencies and was recommended by aseering on GitHub.
you can launch bash via run.exe: run.exe bash.exe -c "<whatever Linux command>". (run.exe is available here: http://www.straightrunning.com/projectrun/ , make sure you download the 64-bit version, the 32-bit version will not be able to find or run bash).
With run on the search PATH, you can just call
run bash -c "DISPLAY=:0 xmessage hello"
So I just made this workaround for now. I really hope that there's a better way than this, but here it goes:
In the command prompt that lives purely to keep WSL alive, I have this script running:
wsl_run_server
#!/bin/bash
set -e
nc -kl 127.0.0.1 15150 | sh
And then I have this command to execute commands in background:
wsl_run_command
if ! pidof -x bin/wsl_run_server; then
echo wsl_run_server isnt running!
exit 1
fi
echo \($#\) \& | nc localhost 15150
from windows I then call:
bash -c "DISPLAY=:0 ~/bin/wsl_run_command xmessage hello"
There no longer is a need to have that command window pop up anymore with WSLg recently added to the mix. You just can call bash using WSLg, like so (I use Ubuntu currently in WSL):
wslg ~ -d Ubuntu bash
This will create a BASH session that will just sit there without being seen. Alternatively, you can do what I do and run a few services that stay running. I created a script that checks for running services, and if it doesn't find them running, will run them. Create the file in /usr/bin:
sudo touch /usr/bin/start-wsl-services
sudo nano /usr/bin/start-wsl-services
Past the following into the file:
#!/bin/bash
# Check for and run System-wide DBus service.
SERVICE="dbus-daemon"
if pgrep -x "$SERVICE" >/dev/null
then
pgrep -a "$SERVICE"
else
sudo /etc/init.d/dbus start
pgrep -a "$SERVICE"
fi
# Check for and run CUPS Printing Service.
SERVICE="cupsd"
if pgrep -x "$SERVICE" >/dev/null
then
pgrep -a "$SERVICE"
else
sudo /etc/init.d/cups start
pgrep -a "$SERVICE"
fi
# Check for and start Freshclam CLAMAV Update service.
SERVICE="freshclam"
if pgrep -x "$SERVICE" >/dev/null
then
pgrep -a "$SERVICE"
else
sudo /etc/init.d/clamav-freshclam start
pgrep -a "$SERVICE"
fi
# Check for and start SANED Scanner service.
SERVICE="saned"
if pgrep -x "$SERVICE" >/dev/null
then
pgrep -a "$SERVICE"
else
sudo /etc/init.d/saned start
pgrep -a "$SERVICE"
fi
# Check for and start screen-cleanup service.
SERVICE="screen-cleanup"
if pgrep -x "$SERVICE" >/dev/null
then
pgrep -a "$SERVICE"
else
sudo /etc/init.d/screen-cleanup start
pgrep -a "$SERVICE"
fi
# Check for and start Preload service.
SERVICE="preload"
if pgrep -x "$SERVICE" >/dev/null
then
pgrep -a "$SERVICE"
else
sudo /etc/init.d/preload start
pgrep -a "$SERVICE"
fi
# Prestart LibreOffice twice for faster loading.
#/usr/bin/libreoffice --terminate_after_init
#sleep 5
#/usr/bin/libreoffice --terminate_after_init
# Check for error, make sure all functions called and run, and pass the result on to calling process.
if [[ $? -ne 0 ]] ; then
exit 1
else
exit 0
fi
Save and exit the file, and then make it executable:
sudo chmod +x /usr/bin/start-wsl-services
I then call this using a shortcut that runs a startup script at startup. Or you can just run it manually. The command I use in the startup script is:
C:\Windows\System32\wslg.exe -d Ubuntu -- /usr/bin/start-wsl-services
The startup command script I use (named StartWSL.cmd) is as follows:
#echo off
echo Starting WSL Linux...
:RETRY
C:\Windows\System32\wslg.exe -d Ubuntu -- /usr/bin/start-wsl-services
REM - C:\Windows\System32\bash.exe -c '/usr/bin/start-wsl-services'
IF %ERRORLEVEL% NEQ 0 (GOTO RETRY)
REM - Allow time to see all results.
timeout /t 5 /nobreak >NUL
REM - Uncomment below line for troubleshooting.
REM - pause
exit 0
And that's how I now keep WSL running in the background on Windows 11, and similar to how I did it on Windows 10.
run command background
screen -dmS [name] [command]
example
screen -dmS gui bash -c "DISPLAY=:0 xmessage hello"
create a shortcut on windows desktop(run in wsl)
wslusc screen -dmS gui bash -c "DISPLAY=:0 xmessage hello"
I have a go web app compiled to a single binary that I am trying to manage via init.d. Here is my init.d script:
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/bin/my-go-app
DAEMON_ARGS="--logFile /var/log/my-go-app/my-go-app.log"
NAME=my-go-app
DESC=my-go-app
RUNDIR=/var/run/my-go-app
PIDFILE=$RUNDIR/my-go-app.pid
test -x $DAEMON || exit 0
if [ -r /etc/default/$NAME ]
then
. /etc/default/$NAME
fi
. /lib/lsb/init-functions
set -e
case "$1" in
start)
echo -n "Starting $DESC: "
mkdir -p $RUNDIR
touch $PIDFILE
chown ubuntu:ubuntu $RUNDIR $PIDFILE
chmod 755 $RUNDIR
if [ -n "$ULIMIT" ]
then
ulimit -n $ULIMIT
fi
if start-stop-daemon --start --quiet --umask 007 --pidfile $PIDFILE --chuid ubuntu:ubuntu --exec $DAEMON -- $DAEMON_ARGS
then
echo "$NAME."
else
echo "failed"
fi
;;
stop)
echo -n "Stopping $DESC: "
if start-stop-daemon --stop --retry forever/TERM/1 --quiet --oknodo --pidfile $PIDFILE --exec $DAEMON
then
echo "$NAME."
else
echo "failed"
fi
rm -f $PIDFILE
sleep 1
;;
restart|force-reload)
${0} stop
${0} start
;;
status)
echo -n "$DESC is "
if start-stop-daemon --stop --quiet --signal 0 --name ${NAME} --pidfile ${PIDFILE}
then
echo "running"
else
echo "not running"
exit 1
fi
;;
*)
echo "Usage: /etc/init.d/$NAME {start|stop|restart|force-reload|status}" >&2
exit 1
;;
esac
exit 0
The problem is that when I run service my-go-app start, it just hangs, like this:
ubuntu#ip-10-0-0-40:~$ service my-go-app start
Starting my-go-app:
and never returns. In this state, if I open a separate terminal, I can see that the app is running by checking the log file but there is nothing in /var/run/my-go-app/my-go-app.pid (though the pid file does get created).
Has anyone encountered (and hopefully resolved) this before? How can I run my go app as an init.d daemon?
EDIT:
I was able to get this to work by adding the "-b -m" command line flags to start-stop-daemonwhen starting the service. That line now looks like this:
start-stop-daemon -b -m --start --quiet --umask 007 --pidfile $PIDFILE --chuid ubuntu:ubuntu --exec $DAEMON -- $DAEMON_ARGS
My concern with this approach is the warning in the start-stop-daemon manpage:
-b, --background
Typically used with programs that don't detach on their own. This option will force start-stop-daemon to fork before starting the process, and
force it into the background. WARNING: start-stop-daemon cannot check the exit status if the process fails to execute for any reason. This is
a last resort, and is only meant for programs that either make no sense forking on their own, or where it's not feasible to add the code for
them to do this themselves.
This seems like a bad idea to me, because it sounds like SysV won't know if the process dies. Am I understanding this correctly? Has anyone else tried this approach?
If you are running a system with Upstart you can use this script:
start on runlevel [2345]
stop on runlevel [016] or unmounting-filesystem
# Replace {soft} and {hard} with the soft and hard resource limits you desire
#limit nofile {soft} {hard}
umask 007
setuid ubuntu
setgid ubuntu
exec /usr/bin/my-go-app --logFile /var/log/my-go-app/my-go-app.log
You can also add the following code to your daemon at a point where your application has been started and initialized correctly:
if ("yes" == os.Getenv("MYAPP_RAISESTOP")) {
p, err := os.FindProcess(os.Getpid())
p.Signal(syscall.SIGSTOP)
}
and the following to lines to the above upstart job:
env MYAPP_RAISESTOP="yes"
expect stop
I am sorry if the if () { } is not real Go syntax; I am a C programmer haha (although the stuff inside the () and {} is real, I did a little research :).
Doing this last bit ensures that Upstart will wait until your application is set up correctly before firing off the started event. If no other jobs are waiting for your app, then you do not really need that.
You will need the --background flag if you want to use SysVinit and start-stop-daemon with Go programs.
I suggest using something like Supervisor or Circus instead.
Edit:
This is not strictly true, if your Go program daemonizes its self, the --background flag can be excluded.
Hi I am creating a new init script for monitoring a ruby program .
NAME=differ
FILE_PATH=/home/amer/Documents/ruby_projects/differ/
PIDFILE=/home/amer/pid/differ.pid
PID=$$
EXEC='/home/amer/.rvm/rubies/ruby-2.0.0-p247/bin/ruby main_scheduler.rb'
do_start(){
echo "started"
cd $FILE_PATH
pwd
$EXEC >> init_log/output.log &
echo $! > $PIDFILE
echo "---------"
echo `cat $PIDFILE`
echo "all are DONE "
}
do_stop(){
PID=`cat $PIDFILE`
echo $PID
if ps -p $PID ; then
kill -6 $PID
echo "it is over"
else
echo "its not running"
fi
}
case "$1" in
start)
echo $$
echo -n "Starting script differ "
do_start
;;
stop)
echo "stopping ...."
do_stop
;;
status)
PID=`cat $PIDFILE`
echo "STATUS $PID"
if ps -p $PID -f; then
echo running
else
echo not running
fi
;;
restart|reload|condrestart)
do_stop
do_start
;;
*)
echo "Usage: /etc/init.d/blah {start|stop}"
exit 1
;;
esac
exit 0
And my monit process is
check process differ with pidfile /home/amer/pid/differ.pid
if changed pid then exec "/etc/init.d/differ start"
start program = "/etc/init.d/differ start"
stop program = "/etc/init.d/differ stop"
if 5 restarts within 5 cycles then timeout
But when I execute start service in my monit the status was "Execution failed" and i checked the log file of monit it said
info : 'differ' start: /bin/bash
error : 'differ' failed to start
error : 'differ' process is not running
When I analyzed the root of the problem . the reason was monit is running as root and the script which executes ruby will be executing as sudo /etc/init.d/differ.sh start but ruby is installed only in user 'amer' . I have tried
sudo -u amer $EXEC >>init_log/output.log &
it displayed the error as
amer#amer-Inspiron-1525:~$ /home/amer/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require': cannot load such file -- bundler/setup (LoadError)
from /home/amer/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require'
from main_scheduler.rb:2:in `<main>'
Please help in this problem . I have two ruby versions.
/home/amer/.rvm/rubies/ruby-2.0.0-p247/bin/ruby
/home/amer/.rvm/bin/ruby
It looks like your environment is missing. Replace
sudo -u amer $EXEC >>init_log/output.log &
with
su -s /bin/bash - amer -c "$EXEC >> init_log/output.log 2>&1" &
This should setup your shell environment properly. If you ran sudo .. >> log before, the log file might be owned by root. Change that or it will fail. I also added the redirect of STDERR to STDOUT, since you are probably interested in error messages.
after a long struggle i found the solution for this problem .
Two things must be done
1) EXPORT your PATH,GEM_HOME,GEM_PATH in the script
export PATH="/home/amer/.rvm/gems/ruby-2.0.0-p247#rails329/bin:/home/amer/.rvm/gems/ruby-2.0.0-p247#global/bin:/home/amer/.rvm/rubies/ruby-2.0.0-p247/bin:/home/amer/.rvm/bin:/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"
export GEM_HOME=/home/amer/.rvm/gems/ruby-2.0.0-p247#rails329
export GEM_PATH=/home/amer/.rvm/gems/ruby-2.0.0-p247#rails329:/home/amer/.rvm/gems/ruby-2.0.0-p247#global
2) USE rvmsudo bundle exec ruby "filename" (use full path)
rvmsudo -u amer /home/amer/.rvm/gems/ruby-2.0.0-p247#rails329/bin/bundle exec /home/amer/.rvm/rubies/ruby-2.0.0-p247/bin/ruby main_scheduler.rb&
it worked for me . hope it does for everyone .
Here's what I do when I want to run ruby scripts in init:
I switch to super user and install rvm. This won't cause problems with your single user installation.
I put this in the init script:
/usr/local/rvm/bin/rvm-shell 'yourgemset' -c 'ruby pathtoyourscript/yourscript.rb'
Example:
/usr/local/rvm/bin/rvm-shell 'jruby-1.7.4' -c 'ruby /home/someone/service.rb'
Hint: all the necessary gems need to be installed in that gemset.
The proper way of doing all this is to create an rvm wrapper script (see example) but I find my method easier for a simple setup where there aren't many gemsets.
I've got a SHOUTcast server running on Ubuntu. The server process is running great, but I can't seem to get the daemon script to function properly. Following a couple tutorials I found I came up with this:
#!/bin/sh
CONFIG="/home/apps/shout32/sc_plex.conf"
DAEMON="/home/apps/shout32/sc_serv"
case "$1" in
start)
echo "Starting SC..."
$DAEMON $CONFIG > /dev/null 2>&1 &
;;
stop)
echo "Stopping SC..."
kill -9 `ps -C sc_serv -o pid --no-headers`
;;
restart)
echo "Rebooting SC..."
kill -9 `ps -C sc_serv -o pid --no-headers`
$DAEMON $CONFIG > /dev/null 2>&1 &
;;
*)
echo "usage: service sc32d {start | stop | restart}"
exit 1
;;
esac
This however does not work. I didn't know what a lot of this meant, so I started to break it down line by line. If I remove the /dev/null stuff - which as I now understand keeps the program running 'silent' in the background - I get this message, and the program closes:
root#streams3:/etc/init.d# service sc32d start
Starting SC...
root#streams3:/etc/init.d# 2013-05-21 14:41:50 E msg:<***> logger could not open file logs/sc_serv.log
2013-05-21 14:41:50 I msg:<***> Logger shutdown
root#streams3:/etc/init.d#
root#streams3:/etc/init.d# ps -C sc_serv
PID TTY TIME CMD
root#streams3:/etc/init.d#
I was still in the process of researching what exactly /dev/null did and why, so I wanted to run those commands with all the /dev/null stuff by hand, which I did, and that's where I got some sort of error code:
root#streams3:/etc/init.d# /home/apps/shout32/sc_serv /home/apps/shout32/sc_plex.conf > /dev/null 2>&1 &
[2] 2261
root#streams3:/etc/init.d#
[2]- Exit 255 /home/apps/shout32/sc_serv /home/apps/shout32/sc_plex.conf > /dev/null 2>&1
root#streams3:/etc/init.d# ps -C sc_serv
PID TTY TIME CMD
Unfortunately from the brief amount of research I did it sounds like 'Exit 225' is like a catch-all error code for codes that are outside of the acceptable range of codes.
The interesting part of the whole issue is this: When I navigate to the /home/apps/shout32/ folder, and run the commands there, without the full path... damn thing works:
root#streams3:/home/apps/shout32# ./sc_serv sc_plex.conf > /dev/null 2>&1 &
[2] 2245
root#streams3:/home/apps/shout32#
root#streams3:/home/apps/shout32# ps -C sc_serv
PID TTY TIME CMD
2245 pts/0 00:00:00 sc_serv
So, something is messing up because the script file is in /etc/init.d/ and not in the folder the application is in? As far as I know I followed every step in the published tutorials for setting up SHOUTcast in Ubuntu and then making a daemon... I don't think I missed anything. I have a feeling the solution is either staring me right in the face or its some sort of obscure permissions thing that's a bit over my head.
But any help would be greatly appreciated!
So, based on an answer below I added cd /home/apps/shout32/ to the START command in my script, also added pwd and ls... to see if we could eliminate the fact that the script couldn't find the /log/ directory.
So now my script is:
CONFIG="/home/apps/shout32/sc_plex.conf"
DAEMON="/home/apps/shout32/sc_serv"
cd /home/apps/shout32/
case "$1" in
start)
echo "Starting SC..."
cd /home/apps/shout32/
pwd
ls
$DAEMON $CONFIG &
;;
stop)
echo "Stopping SC..."
kill -9 `ps -C sc_serv -o pid --no-headers`
;;
restart)
echo "Rebooting SC..."
kill -9 `ps -C sc_serv -o pid --no-headers`
$DAEMON $CONFIG &
;;
*)
echo "usage: service sc32d {start | stop | restart}"
exit 1
;;
esac
I got this:
admin#streams3:/etc/init.d$ service sc32d start
Starting SC...
/home/apps/shout32
changes.txt readme.txt sc_serv_debug.conf
config_builder sc_plex.conf sc_serv_public.conf
control sc_serv sc_serv_relay.conf
docs sc_serv2_linux_07_31_2011.tar sc_serv_simple.conf
logs sc_serv_basic.conf tos.txt
admin#streams3:/etc/init.d$ 2013-06-05 17:52:08 E msg:<***> logger could not open file logs/sc_serv.log
2013-06-05 17:52:08 I msg:<***> Logger shutdown
your second snippet contains logger could not open file logs/sc_serv.log. So it tries to write to a file sc_serv.log which it either expects or wants to create in the directory logs which it expects in the current directory. This also explains that it works when you cd to /home/apps/shout32/ first. I guess there's a file /home/apps/shout32/logs/sc_serv.log.
can you configure the location of that file?
can't you just add some cd ... at the start of the script?
So I wrote the Arch Linux rc.d script for mongod daemon (following an example), but when I do:
sudo rc.d start mongod
it just gets stuck on:
:: Starting /usr/bin/mongod [BUSY]
and never transitions to "DONE" phase. Any tips?
Here is my script:
#!/bin/bash
# import predefined functions
. /etc/rc.conf
. /etc/rc.d/functions
# Point to the binary
DAEMON=/usr/bin/mongod
# Get the ARGS from the conf
. /etc/conf.d/crond
# Function to get the process id
PID=$(get_pid $DAEMON)
case "$1" in
start)
stat_busy "Starting $DAEMON"
# Check the PID exists - and if it does (returns 0) - do no run
[ -z "$PID" ] && $DAEMON $ARGS &> /dev/null
if [ $? = 0 ]; then
add_daemon $DAEMON
stat_done
else
stat_fail
exit 1
fi
;;
stop)
stat_busy "Stopping $DAEMON"
kill -HUP $PID &>/dev/null
rm_daemon $DAEMON
stat_done
;;
restart)
$0 stop
sleep 1
$0 start
;;
*)
echo "usage: $0 {start|stop|restart}"
esac
I've looked at how apache does it, but I can't figure out what they are doing that's different. Here's a piece of their httpd script:
case "$1" in
start)
stat_busy "Starting Apache Web Server"
[ ! -d /var/run/httpd ] && install -d /var/run/httpd
if $APACHECTL start >/dev/null ; then
add_daemon $daemon_name
stat_done
else
stat_fail
exit 1
fi
;;
For one thing, you are passing an $ARGS variable that is never actually defined. You will probably want to either pass some configuration options, or the location of a mongodb.conf file using the -f or --config option, to inform the daemon of the location of your database, log file, IP bindings, etc.
The mongod defaults assume that you database location is /data/db/. If this does not exist, or the daemon does not have permissions to that location, then the init script will fail.
You should probably also run the daemon with a user account other than yourself or root (the default pacman package creates a user named mongodb), and give this user read/write access to the data path and log file.
[ -z "$PID" ] && /bin/su mongodb -c "/usr/bin/mongod --config /etc/mongodb.conf --fork" > /dev/null
I would suggest referring to the mongodb init script provided in the Arch Community package, and comparing that to what you have here. Or, install MongoDB using pacman, which sets all of this up for you.
If all else fails, add some 'echo' commands inside of your if and else blocks to track down exactly where the init script is hanging, check mongodb's logs, and report back to us.