Compare commits

...

7 Commits

Author SHA1 Message Date
d9ec37a4bd Use the cancerous windows task scheduler 2025-06-27 19:00:01 +02:00
e9152da3a9 Update 2025-06-27 18:49:14 +02:00
425b5edbcf Ensure command exists before trying to run 2025-06-27 18:42:16 +02:00
b39ff059be Also initially load crontab jobs 2025-06-27 18:35:43 +02:00
efaa9759c9 Add shims to path 2025-06-27 18:31:15 +02:00
845230529c Ditch "sh -c" since we have no sh 2025-06-27 18:28:21 +02:00
26ec0e38b2 Resolve .cron in cwd 2025-06-27 18:26:27 +02:00
6 changed files with 56 additions and 13 deletions

1
.gitignore vendored
View File

@@ -1 +1,2 @@
*.log
*.cron

2
cron.bat Normal file
View File

@@ -0,0 +1,2 @@
schtasks /Delete /F /TN "InvokeJournal"
schtasks /Create /F /TN "InvokeJournal" /TR "C:\Users\Administrator\scoop\shims\InvokeJournal.exe" /SC MINUTE /MO 10 /RL HIGHEST /RU "Administrator"

3
crontabd/build.sh Normal file
View File

@@ -0,0 +1,3 @@
nssm stop crontabd
go build .
nssm start crontabd

Binary file not shown.

View File

@@ -59,6 +59,11 @@ func main() {
logger.Error("Failed to watch crontab file: %v", err)
return
}
err = loadCronJobs()
if err != nil {
logger.Error("Failed to load initial cron jobs: %v", err)
return
}
for event := range events {
logger.Info("Crontab file updated: %s", event.Name)
// Reload cron jobs when file changes
@@ -98,13 +103,29 @@ func loadCronJobs() error {
cronExpr := strings.Join(fields[0:5], " ")
cmdStr := strings.Join(fields[5:], " ")
cmdStr, err = filepath.Abs(cmdStr)
if err != nil {
logger.Error("Failed to find command: %s", cmdStr)
continue
}
cmdStr = filepath.Clean(cmdStr)
file, err := os.OpenFile(cmdStr, os.O_RDONLY, 0644)
if err != nil {
logger.Error("Failed to find command: %s", cmdStr)
continue
}
file.Close()
logger.Info("Adding cron job: %s -> %s", cronExpr, cmdStr)
job := CronJob{
Cron: cronExpr,
Command: cmdStr,
}
// Add the job to cron
_, err := cronRunner.AddFunc(job.Cron, createJobFunc(job))
_, err = cronRunner.AddFunc(job.Cron, createJobFunc(job))
if err != nil {
logger.Error("Failed to add cron job %+v: %v", job, err)
continue
@@ -119,7 +140,8 @@ func loadCronJobs() error {
func createJobFunc(job CronJob) func() {
return func() {
cmd := exec.Command("sh", "-c", job.Command)
logger.Info("Executing command: %s", job.Command)
cmd := exec.Command(job.Command)
output, err := cmd.CombinedOutput()
if err != nil {
logger.Error("Failed to execute command '%s': %v", job.Command, err)
@@ -133,17 +155,31 @@ func createJobFunc(job CronJob) func() {
func resolveCrontabPath() (string, error) {
// Resolve crontab file location
homeDir, err := os.UserHomeDir()
// homeDir, err := os.UserHomeDir()
// if err != nil {
// return "", fmt.Errorf("failed to get user home directory: %v", err)
// }
// userName := os.Getenv("USERNAME")
// crontabFile = filepath.Join(homeDir, "crontab", userName+".cron")
// crontabFile, err = filepath.Abs(crontabFile)
// if err != nil {
// return "", fmt.Errorf("failed to get absolute path: %v", err)
// }
// crontabFile = filepath.Clean(crontabFile)
// Never mind this fucking noise
// When running shit with nssm we have to run it with a system account
// And the home of that one is deep within system32
// I'm not going to poke around in system32
// To make it run with the local account requires the password of the account
// Even if the account has no fucking password
// So we're just going to look for it in the cwd and call it a day
cwd, err := os.Getwd()
if err != nil {
return "", fmt.Errorf("failed to get user home directory: %v", err)
return "", fmt.Errorf("failed to get current working directory: %v", err)
}
userName := os.Getenv("USERNAME")
crontabFile = filepath.Join(homeDir, "crontab", userName+".cron")
crontabFile, err = filepath.Abs(crontabFile)
if err != nil {
return "", fmt.Errorf("failed to get absolute path: %v", err)
}
crontabFile = filepath.Clean(crontabFile)
crontabFile = filepath.Join(cwd, "crontab.cron")
return crontabFile, nil
}

View File

@@ -2,8 +2,9 @@ nssm install crontabd 'C:\Users\Administrator\Seafile\Projects-Go\GoProjects\cro
nssm set crontabd AppDirectory 'C:\Users\Administrator\Seafile\Projects-Go\GoProjects\crontab\crontabd'
nssm set crontabd AppExit Default Restart
nssm set crontabd DisplayName crontabd
nssm set crontabd AppEnvironmentExtra :PATH='C:\Users\Administrator\scoop\shims'
nssm set crontabd ObjectName LocalSystem
nssm set crontabd Start SERVICE_AUTO_START
nssm set crontabd Type SERVICE_WIN32_OWN_PROCESS
nssm set crontabd Type SERVICE_INTERACTIVE_PROCESS
nssm set crontabd AppStdout 'C:\Users\Administrator\Seafile\Projects-Go\GoProjects\crontab\crontabd.log'
nssm set crontabd AppStderr 'C:\Users\Administrator\Seafile\Projects-Go\GoProjects\crontab\crontabd.log'