buildCrontabCommand builds a crontab command with appropriate user flag if needed
(args ...string)
| 928 | |
| 929 | // buildCrontabCommand builds a crontab command with appropriate user flag if needed |
| 930 | func (c Crontab) buildCrontabCommand(args ...string) *exec.Cmd { |
| 931 | if c.User != "" { |
| 932 | // Check if we need to add -u flag (when accessing another user's crontab) |
| 933 | if currentUser, err := user.Current(); err == nil && currentUser.Username != c.User { |
| 934 | // Insert -u flag and username before other arguments |
| 935 | cmdArgs := []string{"-u", c.User} |
| 936 | cmdArgs = append(cmdArgs, args...) |
| 937 | return exec.Command("crontab", cmdArgs...) |
| 938 | } |
| 939 | } |
| 940 | // Default case: run crontab command without -u flag (for current user) |
| 941 | return exec.Command("crontab", args...) |
| 942 | } |