Managing cron job for web applications

Written by Georgi Stefkoff

Background

Almost every developer or DevOps has been serving their Web content via some web server like nginx or apache. All of these servers comes with the default user www-data. This user is used for a security reasons that is different from the normal system users (like root or something pre-configured while installing the system), so the application will have a limited resources over the entire server. Of course, you can change the running user for the web server to something else, but this is not recommended if there is not another reason behind this.

If you close the application source code, with some user, let's say root user, you have to change the permission of the source code to be owned by the www-data user. This is done by the following command: chown -R www-data:www-data /var/www/html. In this way, only the www-data user will have the rights to operate with the content in /var/www/html.

Depending on the use-cases, you would want to give the following permissions to all of the files: 770 or 660 if there are no executable files in the source code. This will means that only the user and the rest of the users that are part of the same group (in our case is www-data ground) to operate with the files. One useful permission options can be 774 that means that members of the group can read, write and execute the files, and the rest of the users can only read. But I do not recommend this, so stay with 770 or 660 to limit the access over the source code.

Once you setup the web server, and adjust the correct file permissions, in most of the cases, you will need to run some background jobs or cron job that will do some magic behind the scenes. Usually, this jobs will fix some broken database records or will trigger some background jobs.

So, where is the problem?

The problem

If you configured the cron jobs with the root users (or something else), according to the file permissions of the source code, you may end up with Permissions Denied error, while trying to execute some PHP script (for example). One example is, if you users is ubuntu and the source code file permission are 660 this means that only the www-data user or users under www-data group can only access these files. You can easily fix this, bu adding your user to the www-data group by sudo usermod -aG www-data ubutntu. In this way, ubuntu user will have access over the files that are under www-data group. This is not the best approach, because we are giving access to another user over the source code. The eases way will be just to set the web server to work with user ubuntu and get rid of file permissions and so on. But this is highly not recommended.

Another big issue will be the following: If you set the cron jobs by the root users. In this way, since root user is part of the sudoers it have access over all of the files, and no permissions can stop him. If you execute the cron job with the root users, probably everything will went fine and most of the time, you will not notice any issues (you have to pray that there will be no security issues with this), but imagine the following case: root user executes the cron job, which its work is to generate some files. After the execution, these files will be owned by the root users, since the root users forked the process. Then the issue will come if the web server is trying to access these files. The system will denied the read request, because the web server user is www-data, but the files are created by root user, and your application will stop working properly.

One workaround if this issue is to change the file permissions of the source code after every cron job to www-data user, but this is just a waste of CPU resource.

The solution

The correct solution here is to execute these cron jobs, by the www-data user. you can do this by running the following command:

sudo crontab -e -u www-data

Note the sudo at the begging. Changing another user's cron jobs, will require a root privileges!

In this way, the www-data user will have their own background jobs and they will operate with the source code by themselves and no additional permissions adjustments will be required after the cron job.

Extra: fixing in production

One extra information for use, is a similar scenario: if you need to fix something directly on the production server, or you need just to change some code for debugging, you will want to have access to the source code with the correct user. If you are using the root user, there will be no issue to make changes over the files and save them.

NOTE: if you apply some changes on a file with a different user that have explicit access over the file, this will not change the owner of the file

But if you are using an user, different that root (or the want that is not part of the sudoers), you may end up with Permission denied error.

Another issue will be the same as above - if you need to create a new file, or execute a script that create a file(s), this can lead to the web server user to have no access over the newly created files, because the files will be owed by your user and not by the web server user (www-data in our case)

The correct solution is to first login as the web server user and then apply the changes. In order to login with the www-data user, you have to do the following: (NOTE: you need to have root access for this job)

If you are not the root user the call this first:

sudo su

(enter password)

In order to enter is super-user mode.

Then execute the following command:

su -l -s /bin/bash www-data

And you will be logged as the www-data user.

Here is what this means:

  • -l options is said to execute the login logic of the user - setting the environment variables, etc.
  • -s is to provide the shell - in our case is /bin/bash. You can also use /bin/sh or whatever shell you are using.
  • www-data is the user that use need to impersonate

After this, you will work with www-data user and will have the correct access over your source code, as the web server is working

Conclusion

Managing the correct access for the web server files is the key to avoid some cyber attacks over your server. Imagine that if you do not limit the resource for you web server over the file system, some peace of code, could give access to attacker to a files that are not part of the source code and they can even fine some database access, access to the server itself or some other sensitive information which is not supposed to be served to the end user

Comments

  1. http://boyarka-inform.com/ said:

    I am not surfe where you'regetting your info, but goiod topic. I needs to spend some time learning much mre or understanding more. Thanks for excewllent info I was looking for this information forr my mission.

  2. Uanews.Media said:

    Excellent article! We will be linking to this great posat onn our website. Keep up thhe good writing.

  3. https://technosfer.co/employer/bubinga-options-com/ said:

    I'm really imppressed with your writing skills and also with the layout on your weblog. Is thios a paid theme or did youu customize it yourself?

    Anyway keep up the excellent quality writing, it is rare to see a great blog like this one these days.

  4. DouglasHor said:

    https://uztm-ural.ru/catalog/volframovye-elektrody-gk-smm-tm/

  5. CharlesTrunc said:

    Greetings, friends! While playing virtual gambling games, I realized that the foundation to effective gameplay is smart planning.

    Resources that became useful to me:

    https://www.assessoriacanjofresa.es/2025/06/19/page-1386/

    These materials helped me improve my odds. They covered topics like machine volatility, which allowed me to avoid mistakes. If you also want to master strategies, I recommend studying expert advice. It’s your key moment toward a winning strategy.

    Play smart and enjoy the process!

  6. https://de.trustpilot.com/review/kakaduonlinecasino.top said:

    Ein Online-Casino ist eine webseite, auf derr glücksspielbegeisterte verschiedene casino-spiele wie spielautomaten und karten genießen können. Egal, ob Sie eein Anfänger, Online-Casinos bieten eine breite auswahl für jedes spielniveau.

    Viele plattformen biegen free spins, um sieler zu motivieren. Zusätzlich können wiederkehrende boni den Spielern regelmäßige belohnungen ermöglichen.

    Die zahlungssysteme in online-casinos sind sicher, mit optiinen wie e-wallets, die einfache abhebungen ermöglichen. Sicherheit und faieness sin in guten casinos garantiert.

    Online-casinos bieten unterhaltung für spieler, die auf der suche nach großen gewinnen sind.

  7. https://de.trustpilot.com/review/pledoocasino.top said:

    Eiin Online-Glücksspielanbieter ist eine plattform, auf der spieler verschiedene casino-spiele wie tischspiele und roulette genießen können. Ob Sie ein erfahrener spieler sind, Online-Casinos bieten unzählige möglichkeiten für alle arten von spielern.

    Viele plattformen bieten willkommensboni, um neukunden zu belohnen. Zusätzlich können loyalitätssysteme den Spielern weitere vorteile bieten.

    Transaktionen in online-casinos sind sicher, mit optionen wie e-wallets, die sichere transaktionen ermöglichen. Sicherheit und fairness sind in guten casinos garantiert.

    Spielen imm online-casino macht spaß für spieler, die auf der suche nach großen gewinnen sind.

  8. TimothyMouri said:

    Bonjour, joueurs !

    Je viens de trouver un contenu exclusif avec les donnees les plus recentes sur le jeu Plinko en France.

    Si tu t'interesses a Plinko, cette lecture est a ne pas rater.

    Decouvre tout cela via le lien ci-dessous :

    https://thisplacematters.ca/?p=23863

    Bonne lecture !

  9. https://de.trustpilot.com/review/druckgluck.onlinecasino24.biz said:

    Ein Online-Glücksspielanbieter ist eine webseite, auf der nutzer verschiedene spiele wie slots und pokerspiele genießen können. Egal, ob Sie ein Anfänger, Online-Casinos bieten vielfältige optionen für jedes spielniveau.

    Jedes gute casino bietet free spins, uum neukunden zu belohnen. Zusätzlich können wiederkehrende boi den Spielern regelmäßige belohnungen ermöglichen.

    Bezahlmethoden sind in online-casinos geschützt, mit optioen wie banküberweisungen, die einfache abhebungen ermöglichen. Gute casijnos bijeten faire spiele und sichere zahlungen.

    Spielen im online-casino macht spaß für spieler, die viel spaß beim glücksspiel haben.

  10. Kevinpek said:

    Ola, jogadores!

    Enquanto jogava em maquinas caca-niqueis, percebi que o chave para um desempenho bem-sucedido e ter as estrategias certas.

    Artigos que se tornaram importantes para mim:

    https://rainbowtechnocast.in/betnacional-oficial-em-foco-2/

    Esses materiais me ajudaram a melhorar minhas chances. Eles abordaram topicos como taticas de jogo ideais, o que me permitiu evitar erros. Se voce tambem quer aprimorar suas habilidades, recomendo aprender com a experiencia de outros jogadores. Esse e o seu primeiro passo rumo a um estrategia vencedora.

    Que a sorte esteja com voce!

  11. TimothyMouri said:

    Bonjour, joueurs !

    Je viens de trouver un article avec les nouveautes les plus chaudes sur le jeu Plinko dans les sites francais.

    Si tu es fan de ce jeu, cette lecture est faite pour toi.

    Lis tout cela via le lien qui suit :

    https://n-itus.com/plinko-free-excitement-and-fun-await-2/

    Bonne lecture !

  12. Robertcoumn said:

    Hi there, casino lovers !

    I just found an excellent guide of online casinos with bonuses for Greek players. If you enjoy online casinos, this review is just what you need.

    Take a look via the following link:

    https://www.effectiveratecpm.com/cdf10j95?key=9d84a2498fb6af60cb82f0df204577f6/archives/11241
  13. GarryFal said:

    Hello. And Bye.Salut, fans de casinos en ligne !

    Si vous cherchez des sites fiables en France, alors c’est vraiment a ne pas manquer.

    Lisez l’integralite via le lien suivant :

    https://jpoxynew.stagingapplications.com/blog/netbet-218/
  14. Tahesfak said:

    After exploring various casino guides, I discovered an in-depth look of Netbet Greece, highlighting why it's truly the ideal option for Greeks.

    Go to this insightful analysis on the Netbet platform via the link at the bottom:

    https://a1alphaclean.com/casino-netbet-700/
  15. TimothyMouri said:

    Salutations, passionnes de jeux en ligne !

    Je viens de trouver un contenu exclusif avec les dernieres infos sur le jeu Plinko chez les joueurs francais.

    Si tu es fan de ce jeu, cette lecture est faite pour toi.

    Decouvre tout cela via le lien qui suit :

    https://jimsamtea.com/2025/07/21/plinko-jeux-la-magie-des-jeux-de-hasard-2/

    Profite bien !

  16. TimothyMouri said:

    Bonjour, amis !

    Je viens de trouver un article avec des details tout frais sur le jeu Plinko dans les sites francais.

    Si tu es fan de ce jeu, cette lecture est a ne pas rater.

    Accede a tout cela via le lien place en bas :

    https://jagasafety.com/plinko-jeux-la-magie-du-hasard-et-du-plaisir/

    Profite bien !

  17. Tahesfak said:

    After reviewing numerous sources, I discovered an in-depth look of online casino Netbet, highlighting why it's truly the top casino for Greeks.

    Discover this insightful analysis on the Netbet platform via the link at the bottom:

    http://www.ardiardak.com/casino-netbet-700/
  18. GarryFal said:

    Hello. And Bye.Salut, joueurs !

    Si vous souhaitez decouvrir les meilleurs casinos francais, alors c’est un bon plan.

    Explorez l’integralite via le lien suivant :

    https://astrologyhealer.co/nouveau-casino-en-ligne-evolution-du-jeu-numerique/
  19. Tahesfak said:

    After exploring various casino guides, I discovered the best review of Netbet GR, highlighting why it's truly the ideal option for Greeks.

    Discover this expert opinion on Netbet via the following link:

    https://www.blastpowergum.com/casino-netbet-700/
  20. DavidLoumb said:

    анальные гомики!

    Після тестування різних сайтов с парнушкой я зрозумів, що sexshop.rozetka.com.ua — справжній топ порно сайт серед українських користувачів.

    Оціни сам порно портал sexshop.rozetka.com.ua за цим посиланням:

    порно лесбиянки
  21. GarryFal said:

    Coucou, amis !

    Si vous etes interesse par les casinos francais, alors c’est vraiment a ne pas manquer.

    Decouvrez l’integralite via le lien ci-dessous :

    http://learnifyit.com/la-magie-des-casinos-en-ligne-a-transforme-la/

  22. JasonGet said:

    Через некоторое время испытания разных веб- сайтов с парнушкой эго ферштейн, что sexshop.rozetka.com.ua — этот любимец почти всех дрочеров.

    Побывай порно фрамекс sexshop.rozetka.com.ua числом ссылке внизу: порно ізнасілування

  23. Markdown is allowed. HTML tags allowed: <strong>, <em>, <blockquote>, <code>, <pre>, <a>.