= FreeBSD =
FreeBSD is an advanced computer operating system used to power modern servers, desktops, and embedded platforms. http://www.freebsd.org
 * wget  http://ftp.freebsd.org/pub/FreeBSD/releases/amd64/amd64/ISO-IMAGES/10.3/FreeBSD-10.3-RELEASE-amd64-dvd1.iso

== VirtualBox installation ==
 * Start VirtualBox
 * New
 * Name FreeBSD_10_3
 * Choose type BSD 
 * version FreeBSD 64 bit 
 * RAM 512 MB
 * Create virtual disk dynamic VDI with Fixed size 8 GB
 * Create
 * Start VM
 * Choose ISO FreeBSD-10.3-RELEASE-amd64-dvd1.iso
 * Start 
 * option 1 multi boot 
 * Install 
 * keymap portuguese iso-8859-1 accent keys 
 * continue with pt.iso.acc.kbd keymap 
 * hostname: freebsd 
 * Distribution select: all 
 * Partitioning: auto
 * Partition: entire disk 
 * Partition scheme: GPT guid partition table
 * Partition editor, finish, commit
 * root password: ********
 * Network config: em0
 * configure ipv4: yes 
 * use dhcp: yes 
 * configure ipv6: no 
 * search: <empty>
 * ipv4 dns1: 8.8.8.8
 * ipv4 dns2: 8.8.4.4 
 * is cmos clock set to utc: yes 
 * region: europe 
 * country: portugal mainland 
 * services: sshd dumpdev 
 * add users? yes 
 * username: vitor 
 * several enters 
 * add other account: no
 * exit, apply config and exit installer
 * manual configuration? no 
 * reboot 
 * release ISO 
 * login as root 
{{{
df -h 
traceroute www.sapo.pt
pkg install wget 
# install package management tool ? yes 
proceed with action: yes 
pkg info 
cc --version 
# https://www.freebsd.org/doc/handbook/pkgng-intro.html
pkg search jdk 
pkg install openjdk8-8.92.14_3  #203 MB
# add lines to fstab ...
java -version 
pkg search python 
pkg install python27-2.7.12 # 67MB 
python2.7 --version 
pkg install mongodb-2.6.12 #203MB
pkg install rabbitmq-3.6.2 # 157MB
df -h # avail 3.2 GB
pkg install bash 
pkg install xorg
startx
pkg install windowmaker-0.95.7_2 # 262MB 
echo "exec /usr/local/bin/wmaker" > ~/.xinitrc
startx 
}}}

== Get and burn ISO ==
 * wget  http://ftp.freebsd.org/pub/FreeBSD/releases/amd64/amd64/ISO-IMAGES/10.3/FreeBSD-10.3-RELEASE-amd64-dvd1.iso
 * modprobe sg # for cdrecord
 * cdrecord -scanbus #identify the device DVDRAM LG GP08NU6B
 * cdrecord -dev=6,0,0 -pad -data FreeBSD-10.3-RELEASE-amd64-dvd1.iso
 * # eject the DVD after burning
 * eject
 * cd /
 * mount /dev/sr0 /mnt/cdrom #check DVD
 * cd /mnt/cdrom/packages/FreeBSD:10:amd64/All
 * ls
 * ls -lah | wc -l # 1070 packages python gnome3 kde, qt, mysql, firefox, subversion, xorg, perl, gsl 
 * cd /
 * umount /mnt/cdrom 

== Amazon AMI ==
 * https://aws.amazon.com/marketplace/pp/B00KSS55FY/

== Info severity logging ==
 * add to /etc/rc.conf
{{{
syslogd_enable="YES"
}}}
 * touch /var/log/syslog
 * Add to /etc/syslog.conf
{{{
*.info /var/log/syslog
}}} 

Example program
{{{#!highlight python
#!/usr/bin/python
import threading
import time
import os
import syslog
import datetime
import sys 
import signal

def termHandler(signal,frame):
    print('Signal term caught')
    sys.exit(0)
    
if __name__=="__main__":
    f=open('/var/run/beat.pid','wa')
    f.write('%d'%(os.getpid()))
    f.close()
    signal.signal(signal.SIGTERM,termHandler)    
    
    while True:
        syslog.syslog(syslog.LOG_INFO, "Beat %s"%(datetime.datetime.now()) )
        time.sleep(5)
}}}

== Custom service ==
/home/vitor/beat.py
{{{#!highlight python
#!/usr/bin/python
import threading
import time
import os
import syslog
import datetime
import sys 
import signal

def termHandler(signal,frame):
    print('Signal term caught')
    sys.exit(0)
    
if __name__=="__main__":
    f=open('/var/run/beat.pid','wa')
    f.write('%d'%(os.getpid()))
    f.close()
    signal.signal(signal.SIGTERM,termHandler)    
    
    while True:
        syslog.syslog(syslog.LOG_INFO, "Beat %s"%(datetime.datetime.now()) )
        time.sleep(5)
}}}

In /etc/rc.conf
{{{
beat_enable="YES"
}}}

/etc/rc.d/beat
{{{#!highlight sh
#!/bin/sh

# PROVIDE: beat

. /etc/rc.subr

name=beat
rcvar=beat_enable
command="/usr/local/bin/python2"
command_args="/home/vitor/${name}.py &"

load_rc_config $name

run_rc_command "$1"
}}}

== Give user permission to use su -  ==
 * pw user mod userxyz -G wheel
 * su -

== Bash ==
 * pkg install bash
 * chsh -s /usr/local/bin/bash vitor
 * chsh -s /usr/local/bin/bash root

~/.profile
{{{
#normal user
PS1="\e[0;33m\]\D{%Y-%m-%d}T\A\[\e[m\] \e[0;32m\]\u@\H:\w\r\n\$\[\e[m\] "
}}}

== MongoDB ==
 * mkdir -p /data/db
/etc/rc.conf
{{{
mongodb_enable="YES"
}}}

/etc/rc.d/mongodb
{{{#!highlight sh
#!/bin/sh

# PROVIDE: mongodb

. /etc/rc.subr

name=mongodb
rcvar=mongodb_enable
pidfile="/var/run/mongodb.pid"
command="/usr/local/bin/mongod"
command_args="--pidfilepath=$pidfile --smallfiles --syslog &"

load_rc_config $name

run_rc_command "$1"
}}}
 * check file /usr/local/etc/rc.d/mongod
 * mongod_enable="YES" in /etc/rc.conf
 * mkdir -p /var/db/mongodb 
 * add --smallfiles to mongod_flags in file /usr/local/etc/rc.d/mongod

== RabbitMQ service ==
/etc/rc.conf
{{{
rabbitmqserver_enable=YES
}}}

/etc/rc.d/rabbitmq
{{{#!highlight sh
#!/bin/sh
# PROVIDE: rabbitmq-server

. /etc/rc.subr

name=rabbitmqserver
load_rc_config $name

#rcvar=rabbitmqserver_enable
command="/usr/local/sbin/rabbitmq-server"
pidfile="/var/db/rabbitmq/mnesia/rabbit@freebsd.pid"

if [ $rabbitmqserver_enable = "YES" ];then
  case "$1" in
    start)
      echo "Starting ${name} "
      $command &
      ;;
    status)
      if [ -f $pidfile ];then
        PID=$(cat $pidfile)
        echo "${name} running with PID $PID"
      else
        echo "${name} not running"
      fi
      ;;
    stop)
      if [ -f $pidfile ];then
        PID=$(cat $pidfile)
        echo "Killing ${name} with PID $PID"
        /usr/local/sbin/rabbitmqctl stop
        rm $pidfile
      else
        echo "${name} not running"
      fi
      ;;
    *)
      echo "Usage: /etc/rc.d/rabbitmq {start|status|stop}"
      exit 1
    ;;
  esac
else
  echo "${name} service is not enabled in /etc/rc.conf"
fi

exit 0
}}}
 * check file /usr/local/etc/rc.d/rabbitmq
 * rabbitmq_enable="YES" in /etc/rc.conf

 * /usr/local/sbin/rabbitmq-plugins list
 * /usr/local/sbin/rabbitmq-plugins enable rabbitmq_management #port 15672
 * rabbitmqctl add_user rmq rmq
 * rabbitmqctl  set_user_tags rmq administrator
 * rabbitmqctl set_permissions -p / rmq ".*" ".*" ".*"

 * python -m ensurepip
 * pip install pika

== Vagrant box ==
 * https://app.vagrantup.com/freebsd/boxes/FreeBSD-13.1-STABLE
{{{#!highlight bash
cd /tmp
mkdir freebsd-test
cd freebsd-test
vagrant init freebsd/FreeBSD-13.1-STABLE
vagrant up
vagrant ssh
sudo pkg install openjdk17-17.0.3+7.1
sudo pkg install nano
cat Main.java 
#public class Main{
#  public static void main(String[] args){
#    System.out.println("Main hello world !");
#  }
#}
javac Main.java
java Main
vagrant suspend
}}}