Log in as another user and then exit after finishing the desired work in shell script

Today I met a problem that I tried to log in as another user and them execute my script. Simply using su user or su - user will not work or exit in scripts even if I write exit after that.

I want to exeute himawaripy program for a dynamic wall paper. Using crontab does not work.

The fist solution is using sudo -u user command. This works in that the script exit correctly but the wallpaper does not change.

The second solution is found online:


!#/bin/bash
su - user<<!
command_to_execute
exit
!

This also successfully exit but does not change the wall paper.

I believe it’s the program’s fault instead of the shell’s. ==Still finding why. ==

About the usage:
https://en.wikipedia.org/wiki/Here_document

2017/3/19 20:40