Skip to content

Instantly share code, notes, and snippets.

View misterunix's full-sized avatar
😁
Hey, drop me an email if you like!

Bill Jones misterunix

😁
Hey, drop me an email if you like!
View GitHub Profile
@misterunix
misterunix / amIrunning.go
Created December 31, 2024 01:51
Check if the program is already running. This is to get around doing deamons in Go and use crontab as a hack.
// Check to see if the program is already running. returns true if it is or false if it is not.
func amIrunning(myname string) bool {
currentpid := os.Getpid() // Get the current process ID
currentpidStr := strconv.Itoa(currentpid) // Convert the current process ID to a string
cmd := exec.Command("ps", "aux") // Run the ps aux command to get a list of all running processes
out, err := cmd.CombinedOutput() // Get the output of the command, both srtdout and stderr
if err != nil {
ErrorLogger.Println("Error running ps aux", err)
@misterunix
misterunix / gist:3a041833c733968063bdb04a0a23855b
Last active December 12, 2024 20:10
Check if file exists
package main
import(
"fmt"
"os"
)
func main() {
if fileExists("sample.txt") {
fmt.Println("sample file exists")
} else {
fmt.Println("sample file does not exist")
@misterunix
misterunix / lbForth.c
Created April 29, 2024 01:38 — forked from lbruder/lbForth.c
A minimal Forth compiler in ANSI C
/*******************************************************************************
*
* A minimal Forth compiler in C
* By Leif Bruder <[email protected]> http://defineanswer42.wordpress.com
* Release 2014-04-04
*
* Based on Richard W.M. Jones' excellent Jonesforth sources/tutorial
*
* PUBLIC DOMAIN
*
// copied from https://gist.github.com/unakatsuo - thank you
// IterateIPRange calculates the sequence of IP address from beginAddr to endAddr
// then calls the callback cb for each address of the sequence.
// beginAddr value must be smaller than endAddr.
func IterateIPRange(beginAddr, endAddr net.IP, cb func(addr net.IP)) error {
incIP := func(ip net.IP) net.IP {
for j := len(ip) - 1; j >= 0; j-- {
ip[j]++
if ip[j] > 0 {
break
@misterunix
misterunix / seconds2weeks.go
Created April 26, 2024 02:47
I run some very long tasks and I like to know how long they ran.
// Convert seconds to days, hours, minutes and seconds
func Seconds2Weeks(s float64) string {
var result string
si := int(s)
weeks := si / (7 * 24 * 3600) // get weeks from seconds
si = si % (7 * 24 * 3600)
day := si / (24 * 3600) // get days from seconds
@misterunix
misterunix / 1000separator.go
Created May 5, 2023 13:10
Pretty print, add 1000s separator.
package main
import (
"golang.org/x/text/language"
"golang.org/x/text/message"
)
func main() {
p := message.NewPrinter(language.English)
p.Printf("%d\n", 1000)

load a gzip compressed file in to a slice, line by line.

package main

import (
	"bufio"
	"compress/gzip"
	"log"
	"os"
@misterunix
misterunix / gist:3ace3b5a7e3afb94ceb617193be0e2e2
Last active February 12, 2023 00:23
Convert seconds into a readable time string.

Convert elapsed seconds into a nicely formatted string.

// Convert seconds into a readable time. This is not for real time, only for elapsed time. 
func TimeStr(sec int) (res string) {
	wks, sec := sec/604800, sec%604800
	ds, sec := sec/86400, sec%86400
	hrs, sec := sec/3600, sec%3600
	mins, sec := sec/60, sec%60
	res += fmt.Sprintf("%02dw:", wks)
	res += fmt.Sprintf("%02dd:", ds)