Last active
January 3, 2024 16:35
-
-
Save maoo/2f6db53757be46c61099ebb5f3ef8346 to your computer and use it in GitHub Desktop.
Script to manage soffice service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Copied from https://gist.github.com/bordin/80aafced80fcb8358c2951baa39c3642 and adapted | |
# Libre Office | |
ALF_DATA="/opt/alfresco-community/alf_data" | |
SOFFICE_PATH="/opt/alfresco-community/libreoffice/program" | |
SOFFICE_PORT="8100" | |
SOFFICEBIN=${SOFFICE_PATH}/.soffice.bin | |
SOFFICEWRAPPER=${SOFFICE_PATH}/soffice.bin | |
SOFFICE="$SOFFICEWRAPPER --accept=socket,host=127.0.0.1,port=${SOFFICE_PORT};urp;StarOffice.ServiceManager -env:UserInstallation=file://${ALF_DATA}/oouser --headless --nocrashreport --nofirststartwizard --nologo --norestore" | |
SOFFICE_STATUS="" | |
ERROR=0 | |
get_pid() { | |
PID="" | |
PIDFILE=$1 | |
# check for pidfile | |
if [ -f $PIDFILE ] ; then | |
PID=`cat $PIDFILE` | |
echo "Found PID: $PID" | |
fi | |
} | |
is_service_running() { | |
PID=$1 | |
if [ "x$PID" != "x" ] && kill -0 $PID 2>/dev/null ; then | |
RUNNING=1 | |
else | |
RUNNING=0 | |
fi | |
return $RUNNING | |
} | |
is_soffice_running() { | |
SOFFICE_CPU=$(top -b -d1 -n1 | grep -m1 soffice | awk '{print $9}' | cut -f1 -d".") | |
SOFFICE_PID=$(top -b -d1 -n1 | grep -m1 soffice | awk '{print $1}') | |
echo "soffice CPU occupation is $SOFFICE_CPU" | |
if [[ "$SOFFICE_CPU" -gt "80" ]]; then | |
echo "CPU is higher than 80%, killing process $SOFFICE_PID" | |
kill $SOFFICE_PID | |
sleep 10 | |
kill -9 $SOFFICE_PID | |
fi | |
pids=`ps ax | grep $SOFFICEBIN | grep -v grep | awk {'print $1'}` | |
if [ -n "$pids" ]; then | |
RUNNING=1 | |
else | |
RUNNING=0 | |
fi | |
if [ $RUNNING -eq 0 ]; then | |
SOFFICE_STATUS="libreoffice not running" | |
else | |
SOFFICE_STATUS="$(date) - libreoffice already running" | |
fi | |
return $RUNNING | |
} | |
start_soffice() { | |
is_soffice_running | |
RUNNING=$? | |
if [ $RUNNING -eq 1 ]; then | |
echo "$0 $ARG: $(date) - libreoffice already running" | |
else | |
$SOFFICE >/dev/null 2>&1 & | |
sleep 3 | |
is_soffice_running | |
RUNNING=$? | |
if [ $RUNNING -eq 0 ]; then | |
ERROR=1 | |
fi | |
if [ $ERROR -eq 0 ]; then | |
echo "$0 $ARG: libreoffice started at port $SOFFICE_PORT" | |
sleep 2 | |
else | |
echo "$0 $ARG: libreoffice could not be started" | |
ERROR=3 | |
fi | |
fi | |
} | |
daemon_soffice() { | |
$SOFFICE >/dev/null 2>&1 | |
} | |
stop_soffice() { | |
NO_EXIT_ON_ERROR=$1 | |
is_soffice_running | |
RUNNING=$? | |
if [ $RUNNING -eq 0 ]; then | |
echo "$0 $ARG: $SOFFICE_STATUS" | |
if [ "x$NO_EXIT_ON_ERROR" != "xno_exit" ]; then | |
exit | |
else | |
return | |
fi | |
fi | |
pids=`ps ax | grep $SOFFICEBIN | grep -v grep | awk {'print $1'}` | |
if kill $pids ; then | |
echo "$0 $ARG: libreoffice stopped" | |
else | |
echo "$0 $ARG: libreoffice could not be stopped" | |
ERROR=4 | |
fi | |
} | |
if [ "x$1" = "xstart" ]; then | |
start_soffice | |
elif [ "x$1" = "xdaemon" ]; then | |
daemon_soffice | |
elif [ "x$1" = "xstop" ]; then | |
stop_soffice | |
elif [ "x$1" = "xstatus" ]; then | |
is_soffice_running | |
echo "$SOFFICE_STATUS" | |
fi | |
exit $ERROR |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment