0

I created a bash script scriptToCreateFilesWithSystemD.sh to create a file inside a directory dir4systemDfiles
Both have read-write access for all and located in home directory

I added this code inside the bash file:

[rajparayane@localhost ~]$ cat scriptToCreateFilesWithSystemD.sh
#!/bin/bash
touch /home/rajparayane/dir4systemDfiles/filecreated.$(date +"%d-%m-%Y_%I:%M:%S").txt

It creates files inside the dir dir4systemDfiles with date and time

when I tried running the bash file it worked fine

[rajparayane@localhost ~]$ ./scriptToCreateFilesWithSystemD.sh
[rajparayane@localhost ~]$ ll dir4systemDfiles/
total 0
-rw-rw-r--. 1 rajparayane rajparayane 0 Jan 2 20:42 filecreated.02-01-2023_08:42:34.txt

Then I created a fileCreatorSystemD1.service file to run this script automatically by placing it inside /etc/systemd/system

[rajparayane@localhost system]$ sudo cat fileCreatorSystemD1.service
[Service]
ExecStart=/home/rajparayane/scriptToCreateFilesWithSystemD.sh

The fileCreatorSystemD1.service was automatically loaded in the systemctl list-unit-files list

Then I ran the systemctl daemon-reload command

The status of the fileCreatorSystemD1.service:

[rajparayane@localhost system]$ sudo systemctl status fileCreatorSystemD1
● fileCreatorSystemD1.service
Loaded: loaded (/etc/systemd/system/fileCreatorSystemD1.service; static; vendor preset>
Active: inactive (dead)

Then I ran the command sudo systemctl start fileCreatorSystemD1

It started the service but now its status shows Active: failed

[rajparayane@localhost system]$ sudo systemctl status fileCreatorSystemD1
● fileCreatorSystemD1.service
Loaded: loaded (/etc/systemd/system/fileCreatorSystemD1.service; static; vendor preset>
Active: failed (Result: exit-code) since Mon 2023-01-02 20:27:05 IST; 2s ago
Process: 4089 ExecStart=/home/rajparayane/scriptToCreateFilesWithSystemD.sh (code=exite>
Main PID: 4089 (code=exited, status=203/EXEC)

Also It didn't execute the bash file scriptToCreateFilesWithSystemD which was mentioned in the fileCreatorSystemD1.service

sudo cat fileCreatorSystemD1.service
[Service]
ExecStart=/home/rajparayane/scriptToCreateFilesWithSystemD.sh

  • One possibility: https://unix.stackexchange.com/a/583588/117549 – Jeff Schaller Jan 02 '23 at 16:42
  • And https://unix.stackexchange.com/a/665036/117549 – Jeff Schaller Jan 02 '23 at 16:42
  • And https://unix.stackexchange.com/a/490707/117549 – Jeff Schaller Jan 02 '23 at 16:43
  • @JeffSchaller It worked !! Thank You. From your suggested link: https://unix.stackexchange.com/a/665036/117549 I tried to move my program scriptToCreateFilesWithSystemD.sh from home to /user/local/bin/ and ran the command restorecon -Rv /usr/local/bin which responded with Relabeled /usr/local/bin/scriptToCreateFilesWithSystemD.sh from unconfined_u:object_r:user_home_t:s0 to unconfined_u:object_r:bin_t:s0 – Raj Parayane Jan 02 '23 at 17:41
  • @JeffSchaller can you explain in simple terms what is the meaning of this line Relabeled /usr/local/bin/scriptToCreateFilesWithSystemD.sh from unconfined_u:object_r:user_home_t:s0 to unconfined_u:object_r:bin_t:s0 – Raj Parayane Jan 02 '23 at 18:03
  • I'm happy to hear that my breadcrumbs led you to a solution (or work-around)! The simplest thing to do at this point would be to close this question as a duplicate of https://unix.stackexchange.com/q/664811/117549, since that Answer solved this problem. I wasn't sure initially what the problem was; without more evidence in this question, it'd be hard to write a proper Answer. – Jeff Schaller Jan 03 '23 at 03:04
  • Re: the restorecon "Relabeld..." message, I'd recommend checking the man page for restorecon as well as an introduction to SE Linux. If you're still confused, I'd recommend opening a new Question. – Jeff Schaller Jan 03 '23 at 03:07

0 Answers0