2

I have CentOS 7.6 on a virtual machine. there some bash script at /opt which i need to run them with this user dbuser. these scripts write their output in a folder. Using crontab -e I put these in dbuser crontab so they run every minute:

* * * * * /opt/share/scripts/Connections
* * * * * /opt/share/scripts/Memory
* * * * * /opt/share/scripts/Regular

when i check the output folder they are not changed every minute. when i run the scripts manually they run without problem and their output is updated correctly.
i checked all of directories and sub-directories to these scripts and they all have the full x permission.
in /var/log/cron it says that commands are run and there is no error.
i also restarted crond service after each change.
i also tried these:

 * * * * * /usr/bin/bash /opt/share/scripts/Connections
 * * * * * /usr/bin/sh /opt/share/scripts/Connections
 * * * * * dbuser /opt/share/scripts/Connections

and i still get no update in output folders(these commands works fine manually).
finally i added this in root crontab and it is working correctly without error and updates the output files:

* * * * * /usr/sbin/runuser -l dbuser -c '/opt/share/scripts/Connections'  

i can't add every user's crontab to root. i want them to be in their own crontab. why this happen and how i can fix it?

UPDATE
the scripts write to a place where user dbuser has permission to write. i tested it.
this user is a database user so in the scripts i have the path of database command:
DB2PATH=/home/$username/sqllib/bin/db2
should i add this to the path that cronjob use?
no it doesn't send any mail. they produce reports of database status that is for later refer.

8
  • There is no information about the scripts themselves in the question. Do they write to a place where dbuser has permission to write, for example? Do they use tools that may not be in the PATH used by cronjobs? Does the cron daemon send error messages to the dbuser user by mail, which it usually does if a job produces output or errors?
    – Kusalananda
    Feb 22, 2020 at 16:59
  • Do they use environment variables or a custom PATH that's set up in dbusers profile?
    – roaima
    Feb 22, 2020 at 17:42
  • @roaima no they don't. Feb 24, 2020 at 3:08
  • @Kusalananda i updated my question. Feb 24, 2020 at 3:15
  • 1
    @roaima by crontab -e. i never restarted crond before. here i was just testing if it solve the problem or not. Feb 24, 2020 at 6:48

1 Answer 1

1

You can define the user via su - statement. I had the same issue initially with userscripts - this worked for me:

* * * * *   root    su - <username> -c "bash <script to run>"
1
  • 1
    If you are using the system crontab in /etc/crontab, change the username in column six from root to the user that you want to run the bash script as, rather than using su. Doing so would simplify the scheduling.
    – they
    Sep 28, 2021 at 11:59

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.