Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linux Daemon #586

Closed
doktor1360 opened this issue Apr 5, 2017 · 17 comments
Closed

Linux Daemon #586

doktor1360 opened this issue Apr 5, 2017 · 17 comments

Comments

@doktor1360
Copy link

doktor1360 commented Apr 5, 2017

Knowing this issue (#337) is closed, however I thought I'd add this; not perfect by any means but works like a champ...
file : /etc/init.d/gophish

#!/bin/bash
# /etc/init.d/gophish
# initialization file for stop/start of gophish application server
#
# chkconfig: - 64 36
# description: stops/starts gophish application server
# processname:gophish
# config:/opt/goapps/src/github.com/gophish/gophish/config.json

# define script variables

processName=Gophish
process=gophish
appDirectory=/opt/goapps/src/github.com/gophish/gophish
logfile=/var/log/gophish/gophish.log
errfile=/var/log/gophish/gophish.error

start() {
    echo 'Starting '${processName}'...'
    cd ${appDirectory}
    nohup ./$process >>$logfile 2>>$errfile &
    sleep 1
}

stop() {
    echo 'Stopping '${processName}'...'
    pid=$(/usr/sbin/pidof ${process})
    kill ${pid}
    sleep 1 
}

status() {
    pid=$(/usr/sbin/pidof ${process})
    if [[ "$pid" != "" ]]; then
        echo ${processName}' is running...'
    else
        echo ${processName}' is not running...'
    fi
}

case $1 in
    start|stop|status) "$1" ;;
esac

Change directory to '/etc/init.d/' and make the file executable - 'chmod +x gophish'. Use 'chkconfig --add gophish' and 'chkconfig --levels [0123456] gophish on' to set and configure the init.d process - set the runlevels according to your system. I used 2345 for startup and shutdown scripts. You'll also need to create the '/var/log/gophish' directory accordingly for the log & error files, they'll be created auto-magically for ya when gophish is started.

Hopefully this helps someone out...

@jordan-wright
Copy link
Collaborator

Excellent, thanks @doktor1360! I've gone ahead and added this to the User Guide.

Thanks again for sending this over!

@KhasMek
Copy link

KhasMek commented Apr 27, 2017

This is really handy, thank you for posting it. I do have one nitpick however.

line 1

##!/bin/bash

really needs to be

#!/bin/bash

Otherwise the interpreter isn't invoked properly and the system returns a Exec format error.

@doktor1360
Copy link
Author

doktor1360 commented Apr 27, 2017

Updated the comment - must have been a copy/paste error, wasn't actually in the script - however, agreed... it won't work - bash won't like it all like that...

Thx @KhasMek for spotting it!

@ironc0in
Copy link

Can someone plz post equivalent commands for Ubuntu?

@S0larflare
Copy link
Collaborator

This is a bash script, should be fine in Ubuntu

@ghost
Copy link

ghost commented Jun 8, 2018

Thank you for posting this!

I've used your script as a starting point to set up a systemd .service unit and I thought I could pay back by posting it here.

file: /lib/systemd/system/gophish.service

[Unit]
Description=Gophish service
After=network-online.target

[Service]
Environment="GOPHISH_BIN_PATH=/opt/gophish/"
Environment="GOPHISH_LOG_PATH=/var/log/"
ExecStart=/bin/bash /root/gophish.sh
RestartSec=1
Restart=on-failure

[Install]
WantedBy=multi-user.target

file: /root/gophish.sh

#!/bin/bash

GOPHISH_LOG_FILE=gophish.log
GOPHISH_ERR_FILE=gophish.err

check_bin_path() {
    if [[ -z "$GOPHISH_BIN_PATH" ]]; then
        exit 1
    fi
}

check_log_path() {
    if [[ -z "$GOPHISH_LOG_PATH" ]]; then
        exit 2
    fi
}

create_new_log_err() {
    GOPHISH_STAMP=`date +%Y%m%d%H%M%S-%N`
    if [[ -e $GOPHISH_LOG_PATH$GOPHISH_LOG_FILE ]]; then
        mv $GOPHISH_LOG_PATH$GOPHISH_LOG_FILE $GOPHISH_LOG_PATH$GOPHISH_LOG_FILE-$GOPHISH_STAMP
    fi
    
    if [[ -e $GOPHISH_LOG_PATH$GOPHISH_ERR_FILE ]]; then
        mv $GOPHISH_LOG_PATH$GOPHISH_ERR_FILE $GOPHISH_LOG_PATH$GOPHISH_ERR_FILE-$GOPHISH_STAMP
    fi
    
    touch $GOPHISH_LOG_PATH$GOPHISH_LOG_FILE
    touch $GOPHISH_LOG_PATH$GOPHISH_ERR_FILE
}

launch_gophish() {
    cd $GOPHISH_BIN_PATH
    ./gophish >> $GOPHISH_LOG_PATH$GOPHISH_LOG_FILE 2>> $GOPHISH_LOG_PATH$GOPHISH_ERR_FILE
}

check_bin_path
check_log_path
create_new_log_err
launch_gophish

After everything is in place # chmod +x /root/gophish.sh, then # systemctl daemon-reload. After this use # systemctl normally to start/status/stop/etc. gophish. This will create/stamp separate logs each time the service (re)starts and continuously check for exits on failure in order to restart it.

@Rzqu
Copy link

Rzqu commented Sep 14, 2018

@doktor1360 Hi, for some reason when i try to use your code it returns an error with syntax for a (, Im using a TurnKey Linux distro based on debian, not sure if that makes a difference or not?

@jaredsiegel
Copy link

@dudsan Your instructions worked for me on Ubuntu 16.04.05. Thank you.

@acklenx
Copy link

acklenx commented Oct 16, 2018

@dudsan Thank you for the systemd version. Most critically for any of this to work for me I needed to enable this to run on reboot (which is the entire point for me).
sudo systemctl enable gophish.service

Also for me (running the AWS Ubuntu 18.04 AMI the default install was NOT /opt/gophish - so it was important to change the path to the gophish binary to in the file /lib/systemd/system/gophish.service to:
[Service]
Environment="GOPHISH_BIN_PATH=go/src/github.com/gophish/gophis"

Thank you @doktor1360 for getting this party started. Sorry for posting to closed topic but the "enable" command is just too important to not say something

@manchot0
Copy link

Thanks to @dudsan for the unit script and the bash script. For those who want to run gophish with an unpriviliged user (not root) here is the unit script modified :

[Unit]
Description=Gophish service
After=network-online.target

[Service]
Environment="GOPHISH_BIN_PATH=/opt/gophish-data/"
Environment="GOPHISH_LOG_PATH=/opt/gophish-data/log/"
ExecStart=/bin/bash /opt/gophish-data/gophish.sh

User=gophish
Group=gophish

AmbientCapabilities=CAP_NET_BIND_SERVICE

[Install]
WantedBy=multi-user.target

I have just added the User and Group directive plus the AmbientCapabilities to allow the user to bind to a privileged ports (below 1024).
Of course the user must be the owner of the directory and the files where gophish is installed.

ceramicskate0 added a commit to ceramicskate0/Scripts that referenced this issue Oct 15, 2019
REF:gophish/gophish#586
Change directory to '/etc/init.d/' and make the file executable - 'chmod +x gophish'. Use 'chkconfig --add gophish' and 'chkconfig --levels [0123456] gophish on' to set and configure the init.d process - set the runlevels according to your system. I used 2345 for startup and shutdown scripts. You'll also need to create the '/var/log/gophish' directory accordingly for the log & error files, they'll be created auto-magically for ya when gophish is started.
@MrGoldenpioche
Copy link

In most distribution, pidof tool is in the PATH. I think it is better to simply use "pidof" command in the script instead of specify full path.

@longshanks24
Copy link

I'm running into "service gophish does not support chkconfig" and wondering if someone can point me in the right direction. Fairly new to Linux so this is hurting my brain. I'm following the guide I found here. When I get to the steps that call for "sudo chkconfig gophish on" the not supported chkconfig pops up. I copied and pasted the script from the guide, which was copied and edited based on install location from here. Hoping someone can shed light on what I'm doing wrong.

@doktor1360
Copy link
Author

@longshanks24 - that guide appears to skip over and omit a coupa important commands. The 'chkconfig' command needs to 'add' the service and then set the run 'levels' before turning it 'on' if I'm reading all this correctly. Have a quick look at what I did - it's in the notes in the script at the top of this post...

However, as always:
[Standard Mgmt Disclaimer] - "Your actual mileage may vary..."

@jfarl
Copy link

jfarl commented Sep 16, 2020

I'm running into "service gophish does not support chkconfig" and wondering if someone can point me in the right direction. Fairly new to Linux so this is hurting my brain. I'm following the guide I found here. When I get to the steps that call for "sudo chkconfig gophish on" the not supported chkconfig pops up. I copied and pasted the script from the guide, which was copied and edited based on install location from here. Hoping someone can shed light on what I'm doing wrong.

Try copying the script from this page instead of the linked article.

@olliewen
Copy link

olliewen commented Dec 2, 2021

@dudsan Thank you for the systemd version. Most critically for any of this to work for me I needed to enable this to run on reboot (which is the entire point for me). sudo systemctl enable gophish.service

Also for me (running the AWS Ubuntu 18.04 AMI the default install was NOT /opt/gophish - so it was important to change the path to the gophish binary to in the file /lib/systemd/system/gophish.service to: [Service] Environment="GOPHISH_BIN_PATH=go/src/github.com/gophish/gophis"

Thank you @doktor1360 for getting this party started. Sorry for posting to closed topic but the "enable" command is just too important to not say something

You just need to change the comments in the /etc/init.d to this

#!/bin/bash
### BEGIN INIT INFO
# Provides:          gophish
# Required-Start:    $all
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:
# Short-Description: Starts GoPhish
### END INIT INFO

@aukhan288
Copy link

Is that possible to run gophish without command? like another web application

@Shakura8
Copy link

Shakura8 commented Sep 29, 2024

update, works even on new version of linux 24.04.
If someone have issues with it . just pay attention to the bin Path .
for me it is :
Environment="GOPHISH_BIN_PATH=/opt/gophish/gophish/"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests