Score:0

Cron job launches app in home directory

mv flag

I'm trying to write a script as a watchdog for SA-MP server. It will check every minute whether the server is running, and if not, it will make a copy of server log and start the server. The cron job seems to work. The only problem is that for some reason the server creates server log in /home/pararera, and not in /home/pararera/haswell-samp where server exe file is located.

I guess for some reason app is started in wrong directory and server does not work because it cannot read server.cfg file which is located in home/pararera/haswell-samp. If I manually go to home/pararera/haswell-samp and type ./samp03svr & it works as it should. How can I fix this?

server log is created in /home/pararera

PROCESS_COUNT=$(ps -fu pararera | grep samp03svr | grep -v grep | wc -l)
case $PROCESS_COUNT in
0)
mv server_log.txt /logs/server_log.`date '+%m%d%y%H%M%S'`.txt
/home/pararera/haswell-samp/samp03svr &
;;
1) #OK, program is already running once
;;
*) #OK, program is already multiple times
;;
esac
Pararera avatar
mv flag
@user68186 fixed
hr flag
That is the expected behavior. You can `cd` to the desired directory - either in the crontab, or inside your script. As an aside, consider using `pgrep -c` to count processes ex. `pgrep -cu pararera samp03svr`.
au flag
The working directory for a process isn't determined by where the executable it's running is, it's inherited from the parent process (unless it's specifically set to somewhere else). In the case of user cron jobs, that's generally the user's home folder. You shouldn't expect programs to automatically run in the directory where the executable is located.
Pararera avatar
mv flag
Thanks. I didn't know that.
Pararera avatar
mv flag
`mv` does not work when cron execute the script. If executed manually, it works.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.