@@ -271,7 +271,7 @@ func (c Crontab) Write() string {
271271
272272func (c Crontab ) Save (crontabLines string ) error {
273273 if c .IsUserCrontab {
274- cmd := exec . Command ( "crontab" , "-" )
274+ cmd := c . buildCrontabCommand ( "-" )
275275
276276 // crontab will use whatever $EDITOR is set. Temporarily just cat it out.
277277 cmd .Env = []string {"EDITOR=/bin/cat" }
@@ -335,7 +335,7 @@ func (c Crontab) IsRoot() bool {
335335func (c Crontab ) Exists () bool {
336336
337337 if c .IsUserCrontab {
338- cmd := exec . Command ( "crontab" , "-l" )
338+ cmd := c . buildCrontabCommand ( "-l" )
339339 if _ , err := cmd .CombinedOutput (); err != nil {
340340 return false
341341 }
@@ -357,7 +357,7 @@ func (c Crontab) load() ([]string, int, error) {
357357 return nil , 126 , errors .New ("on Windows, a crontab path argument is required" )
358358 }
359359
360- cmd := exec . Command ( "crontab" , "-l" )
360+ cmd := c . buildCrontabCommand ( "-l" )
361361 if b , err := cmd .CombinedOutput (); err == nil {
362362 crontabBytes = b
363363 } else {
@@ -856,25 +856,33 @@ func ReadCrontabFromFile(username, filename string, crontabs []*Crontab) []*Cron
856856}
857857
858858// GetAllCrontabs returns a slice of all available Crontab objects.
859- func GetAllCrontabs () ([]* Crontab , error ) {
859+ func GetAllCrontabs (users [] string ) ([]* Crontab , error ) {
860860 var crontabs []* Crontab
861- var username string
862861
863- // Get current username for user crontabs
862+ // Get current user for system crontab operations
863+ var currentUser string
864864 if u , err := user .Current (); err == nil {
865- username = u .Username
865+ currentUser = u .Username
866866 }
867867
868- // Read user crontab
869- crontabs = ReadCrontabFromFile (username , CurrentUserCrontab (), crontabs )
868+ // If no users specified, default to current user
869+ if len (users ) == 0 {
870+ users = []string {currentUser }
871+ }
872+
873+ // Read user crontabs for all specified users
874+ for _ , username := range users {
875+ userCrontabPath := fmt .Sprintf ("user:%s" , username )
876+ crontabs = ReadCrontabFromFile (username , userCrontabPath , crontabs )
877+ }
870878
871- // Read system crontab if it exists
872- if systemCrontab := CrontabFactory (username , SYSTEM_CRONTAB ); systemCrontab .Exists () {
873- crontabs = ReadCrontabFromFile (username , SYSTEM_CRONTAB , crontabs )
879+ // Read system crontab if it exists (always use current user)
880+ if systemCrontab := CrontabFactory (currentUser , SYSTEM_CRONTAB ); systemCrontab .Exists () {
881+ crontabs = ReadCrontabFromFile (currentUser , SYSTEM_CRONTAB , crontabs )
874882 }
875883
876- // Read crontabs from drop-in directory
877- crontabs = ReadCrontabsInDirectory (username , DROP_IN_DIRECTORY , crontabs )
884+ // Read crontabs from drop-in directory (always use current user)
885+ crontabs = ReadCrontabsInDirectory (currentUser , DROP_IN_DIRECTORY , crontabs )
878886
879887 return crontabs , nil
880888}
@@ -910,3 +918,18 @@ func (c *Crontab) lightweightCopy() Crontab {
910918 UsesSixFieldExpressions : c .UsesSixFieldExpressions ,
911919 }
912920}
921+
922+ // buildCrontabCommand builds a crontab command with appropriate user flag if needed
923+ func (c Crontab ) buildCrontabCommand (args ... string ) * exec.Cmd {
924+ if c .User != "" {
925+ // Check if we need to add -u flag (when accessing another user's crontab)
926+ if currentUser , err := user .Current (); err == nil && currentUser .Username != c .User {
927+ // Insert -u flag and username before other arguments
928+ cmdArgs := []string {"-u" , c .User }
929+ cmdArgs = append (cmdArgs , args ... )
930+ return exec .Command ("crontab" , cmdArgs ... )
931+ }
932+ }
933+ // Default case: run crontab command without -u flag (for current user)
934+ return exec .Command ("crontab" , args ... )
935+ }
0 commit comments