I have following case which may be is simple but I don't know which way is logically correct and how to do it exactly.

I have multiple sites in /www/ each site is in own directory and user

/www/site1/  // user site1
/www/site2/  // user site2
/www/site3/  // user site3

Now I want to make cronjob which will run PHP script and will update one table in database of each site.

The script and actual job aren't problem. The problem is how to do it properly? How to create for each user job? Cronjob will run every half an hour if it's matter.

2 Answers 2


Add a new user - let's call them allsites.

Add the allsites user to the /etc/group for site1, site2 and site3 users.

Run the script as the allsites user.

Then run a single script with the different details per site included in the script. For (a very basic) example. A copy of script-name.sh is located in each of the $LIST directories:


# The base location of each site
LIST="/path/to/site1 /path/to/site2 /path/to/site3"

# Place script-name.sh in each of the above paths

for i in $LIST
    sh "${i}/${SCRIPT_NAME}"

Another basic example would be something like the following. A single script will pull in a custom config.


# The base location of each site
LIST="/path/to/site1 /path/to/site2 /path/to/site3"

# Place details for each site in config.sh in each of the above paths

for i in $LIST
    # Pull in the config for the current site
    . "${i}/${CONFIG}"
    # Add your commands here that use the details from $CONFIG
    echo "EXAMPLE: user name: $username"

The config.sh which contains the unique details per site would be something like this:


# User name for DB connection??
  • But I should create 3 different files ( with identical scripts inside ) just credentials for database would be different.
    – S.I.
    Oct 19, 2016 at 10:16
  • but like this again is only one script? SCRIPT_NAME="script-name.sh"
    – S.I.
    Oct 19, 2016 at 10:32
  • Added another example. Hope it helps.
    – Tigger
    Oct 19, 2016 at 10:43
  • Just last question. In config.sh I should write dbuser, dbname, dbpass and table, right?
    – S.I.
    Oct 19, 2016 at 11:05
  • Yes, you can add those details to each of the config.sh scripts. If you are using MySQL 5.6+ you should really be using mysql_config_editor to set details and not pass the password on the command line or store it in a file. You then use the --login-path=... option - but that is really another question.
    – Tigger
    Oct 20, 2016 at 4:33

You wrote that:

each site is in own directory and user

/www/site1/  // **user** site1
/www/site2/  // **user** site2
/www/site3/  // **user** site3

On your output username are the same, it's correct?

If users are actually different in your place I create three separate crons for each of them:

for i in user1 user2 user3; do crontab -u $i -e ; done

and threw rule prepared for each of them, for example (where site_dir are your site{1,2,3}:

0,30 * * * * sh -c "cd /www/site_dir/ && /usr/bin/php ./tool.php -c Table_Update"
  • Their databases credentials etc. are different. So that's why they must be different jobs for each one which will load same file replicated 3 times because of diff credentials to db..?
    – S.I.
    Oct 19, 2016 at 10:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.