2

I'm managing a small server at work that we use for Samba and SSH. I'd like to mess around with my coworkers by having a script run at random times of the day where it fetches a random quote (inspired by this) and prints it out to their terminal session. I'd like to make it so that each event is unique to a single user (i.e. one quote doesn't print for everyone simultaneously).

I know how it could be set via Cron at a regular interval but I would have to do this for each user since running it as root would make it print only to root. I suppose I could set the root Cron to run the script as a random user, e.g. * * * * * sudo -u $someuser $random_quote where another script randomly chooses a user to mess with but I want to limit how often each day it occurs per person.

Is there a simpler way to achieve this?


For those who are interested, the script is as follows:

#!/usr/bin/env python
# coding: utf-8
from random import randint
from requests import Session

if __name__ == '__main__':
    limit = 100
    url = 'https://www.reddit.com/r/showerthoughts/top.json?sort=top&t=all&limit=' + str(limit)
    x = randint(0,limit - 1)
    #NEVER lie about your user-agent. - https://github.com/reddit/reddit/wiki/API
    r = Session().get(url, headers = {"user-agent":"Python/RandomQuote4DebuggingFun (by /u/YOUR_REDDIT_USERNAME)"})
    print r.json()['data']['children'][x]['data']['title'].encode()

Just insert your Reddit username at the /u/YOUR_USERNAME_HERE

1 Answer 1

2

I'd start a single control job which will fool your buddies over the day. Add a cronjob starting that script half an hour after start of work and you're done :)

#!/usr/bin/env python
# coding: utf-8
import grp
import random
from time import sleep

#unix group of those users you want to mess around with
FOOL_GROUP = 'sshusers'
MAX_JOKES_PER_USER = 3

def iterUser():
    allUsers = grp.getgrnam(FOOL_GROUP).gr_mem
    allUsers *= MAX_JOKES_PER_USER
    random.shuffle(allUsers)
    for user in allUsers:
        yield user

if __name__ == '__main__':
    for user in iterUser():
        #make your joke here
        #then sleep for up to 20min
        sleep(random.randint(60,1200))
1
  • Just had an idea: create a daemon (using Python) to choose random times and persons to run the Quote generator on. Use Cron to run the script. The script does a check to see if the daemon is running or not (to prevent duplicates). The daemon checks if it's a new day and resets counters if true. Nov 18, 2015 at 21:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .