Compare commits
7 Commits
f31103d1d8
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| d9ec37a4bd | |||
| e9152da3a9 | |||
| 425b5edbcf | |||
| b39ff059be | |||
| efaa9759c9 | |||
| 845230529c | |||
| 26ec0e38b2 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1 +1,2 @@
|
||||
*.log
|
||||
*.cron
|
||||
|
||||
2
cron.bat
Normal file
2
cron.bat
Normal 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
3
crontabd/build.sh
Normal file
@@ -0,0 +1,3 @@
|
||||
nssm stop crontabd
|
||||
go build .
|
||||
nssm start crontabd
|
||||
BIN
crontabd/crontabd.exe
LFS
BIN
crontabd/crontabd.exe
LFS
Binary file not shown.
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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'
|
||||
Reference in New Issue
Block a user